基于springboot的ssm简单框架

搭建一个简单的基于springboot的ssm(spring + springmvc + mybatis)的maven项目,使用的数据库为mysql。

完整项目的目录结构:

目录结构

1 创建项目

1.1 打开IDEA

创建项目-1

1.2 选择Spring Intitializer

创建项目-2

1.3 填写groupid 和artifactid GroupId一般为公司名称,AtifactId为项目名称。

创建项目-3

1.4 先勾选web,其他的后面再添加。

创建项目-4

1.5 选择保存的位置。

创建项目-5

1.6 初次创建时的目录结构

创建项目-6

2 修改pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>ssmdemo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ssmdemo01</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

3 创建目录

目录结构

4 创建数据库

-- 创建数据库
DROP DATABASE IF EXISTS user_test;
CREATE DATABASE user_test;

-- 使用数据库
USE  user_test;

-- 创建用户信息表
CREATE TABLE user_info(
  `user_id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `user_name` VARCHAR(120) NOT NULL COMMENT '用户名称',
  `age` INT NOT NULL COMMENT '用户名称',
  `create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() COMMENT '创建的时间',
  PRIMARY KEY (user_id)
)ENGINE =INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8 COMMENT='用户信息';

-- 插入初始化数据
insert into
  user_info(user_id,user_name,age)
values
  (1001,'小明',21),
  (1002,'小张',22);

数据库

5 添加配置信息,修改application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/user_test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database = mysql

###Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
mybatis.type-aliases-package=com.example.demo.dao

6 在mapper文件夹下添加UserMapper.xml文件

内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.dao.UserMapper" >

        <!--在配置文件中配置过mybatis.type-aliases-package,否则要写全路径, <resultMap id="userMap" type="com.example.demo.dao.User">-->
        <resultMap id="userMap" type="User">
            <id column="user_id" jdbcType="INTEGER" property="userId"/>
            <result column="user_name" jdbcType="VARCHAR" property="userName"/>
            <result column="age" jdbcType="INTEGER" property="age"/>
            <result column="create_time" jdbcType="INTEGER" property="createTime"/>
        </resultMap>
    <sql id="columns">user_id,user_name,age,create_time</sql>


    <select id="selectUserByName"  resultMap="userMap">
        SELECT * FROM user WHERE user_name = #{name}
    </select>



</mapper>

6 dao层

在dao目录下创建User.java和UserMapper.java文件

User.java

package com.example.demo.dao;

public class User {
    private Integer userId;
    private String userName;
    private Integer age;
    private Integer createTime;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Integer createTime) {
        this.createTime = createTime;
    }


}

UserMapper.java

package com.example.demo.dao;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Mapper     //声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可
@Repository
public interface UserMapper {

    User selectUserByName(String name);
}

7 model层

在model文件夹下,建立UserData.java

package com.example.demo.model;

/**
 * Created by fucheng on 2018/4/29.
 */
public class UserData {
    private Integer userId;
    private String userName;
    private Integer age;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

8 service层

在service文件夹下添加,UserService.java和UserServieceImpl.java文件 UserService.java

package com.example.demo.service;

import com.example.demo.model.UserData;

/**
 * Created by fucheng on 2018/4/29.
 */
public interface UserService {

    UserData getUserInfo(String name);
}

UserServiceImpl

package com.example.demo.service.impl;

import com.example.demo.dao.User;
import com.example.demo.dao.UserMapper;
import com.example.demo.model.UserData;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("UserService")
public class UserServiceImpl implements UserService {
    //依赖注入
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserData getUserInfo(String name) {
        UserData userData = new UserData();
        User user = userMapper.selectUserByName(name);
        if(user!=null){
            userData.setUserId(user.getUserId());
            userData.setUserName(user.getUserName());
            userData.setAge(user.getAge());

        }
        return  userData;
    }
}

9 controller层

在controller文件夹下,建立UserController.java

package com.example.demo.controller;

import com.example.demo.dao.UserMapper;
import com.example.demo.model.UserData;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by fucheng on 2018/4/29.
 */
//证明是controller层并且返回json
@RestController
public class UserController {

    @Autowired
    private  UserService userService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/user")
    public UserData selectUserByName() {

        return userService.getUserInfo("小明");
    }
}

10 修改主程序

修改SsmDemo01Application.java

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement//开启事务管理
@MapperScan("com.example.demo.dao")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
public class Ssmdemo01Application {

    public static void main(String[] args) {
        SpringApplication.run(Ssmdemo01Application.class, args);
    }
}

11 运行结果:

运行结果

12 出现的问题

问题:

Sun Apr 29 12:50:15 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决:将数据库配置加上 useSSL=false

spring.datasource.url=jdbc:mysql://localhost:3306/user_test?useUnicode=true&characterEncoding=utf-8&useSSL=false

13 代码下载地址 https://github.com/foochane/ssmdemo01

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券