【SpringBoot】整合SSM框架

学习Spring Boot框架,想必已经感受到该框架带来的快感,接下来教你如何快速搭建Spring、SpringMvc、Mybatis框架整合。

1.项目构建
【SpringBoot】整合SSM框架_第1张图片

2.sql文件

/*
MySQL Backup
Source Server Version: 5.7.13
Source Database: springboot
Date: 2018/3/14 10:28:39
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
--  Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL COMMENT '主键',
  `name` varchar(20) DEFAULT NULL,
  `birthday` date DEFAULT NULL COMMENT '生日',
  `address` varchar(256) DEFAULT NULL COMMENT '地址',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records 
-- ----------------------------
INSERT INTO `user` VALUES ('1','张三','1994-10-20','杭州市'), ('2','李四','1996-02-19','上海市');

3.pom文件


<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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.0.RELEASEversion>
        <relativePath/> 
    parent>

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

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.1version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

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

project>

4.application.properties文件

#database 数据源
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#tomcat端口
server.port=8080

#整合mybatis 
#起别名,省略写mybatis的xml中的resultType的全路径
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
#扫描(配置xml模式使用)
mybatis.type-aliases-package=com.example.pojo

5.mybatis的映射文件UserMapper.xml



<mapper namespace="com.example.mapper.IUserMapper"> 
    <resultMap type="User" id="UserList">
        <result column="id" property="id" />
        <result column="name" property="name" />
        <result column="birthday" property="birthday" />
        <result column="address" property="address" />
    resultMap>

    <select id="queryAllUser" resultMap="UserList">
        SELECT * FROM user
    select>   

    <delete id="deleteUser" parameterType="int">
        DELETE FROM user WHERE id = #{id}
    delete>
mapper>

6.数据交互层Controller

@RestController
@EnableAutoConfiguration
public class IndexController {

    @Autowired
    private IUserService userService;

    /**
     * 整合SSM框架
     */
    @RequestMapping("/ssm")
    public List findAllUser() throws Exception{
        return userService.queryAllUser();
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public void findAllUser(@PathVariable Integer id) throws Exception{
        userService.deleteUser(id);
    }


    /**
     * 返回基本格式JSON格式数据
     * @return
     */
    @RequestMapping(value="/index",produces="text/plain;charset=UTF-8")
    public String index(){
        return "hello spring boot";
    }

    /**
     * 返回POJO对象
     * @return
     */
    @RequestMapping("/pojo")
    public User showUser(){
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setBirthday("1990-02-20");
        user.setAddress("武当山");

        return user;
    }

    /**
     * 返回Map集合对象
     */
    @RequestMapping("/map")
    public Map showMap(){
        Map map = new HashMap();
        map.put("username", "张三丰");
        map.put("gender", "男");
        map.put("username", "赵敏");
        map.put("gender", "女");
        return map;
    } 

    /**
     * 返回List集合对象
     */
    @RequestMapping("/list")
    public List showList(){
        List list = new ArrayList();
        User u1 = new User();
        u1.setId(1);
        u1.setName("张三");
        u1.setAddress("武当山");
        u1.setBirthday("1990-02-20");

        User u2 = new User();
        u2.setId(2);
        u2.setName("李四");
        u2.setAddress("上海市");
        u2.setBirthday("1990-02-21");

        list.add(u1);
        list.add(u2);

        return list;
    } 
}

7.业务逻辑层Service

public interface IUserService {

    List queryAllUser() throws Exception;

    void deleteUser(Integer id) throws Exception;
}

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    private IUserMapper mapper;

    @Override
    public List queryAllUser() throws Exception {
        return mapper.queryAllUser();
    }

    @Override
    public void deleteUser(Integer id) throws Exception {
        mapper.deleteUser(id);
    }

}

8.数据持久层及实体Bean

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

    List queryAllUser() throws Exception;

    void deleteUser(Integer id) throws Exception;
}

public class User implements Serializable {

    private Integer id;
    private String name;
    private String birthday;
    private String address;
    //省略getXxx()、setXxx()
}

9.入口类

@MapperScan("com.example.mapper")
@SpringBootApplication
@EnableTransactionManagement//启注解事务管理,等同于xml配置方式的 
public class DemoApplication {

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

}

运行结果
这里写图片描述

你可能感兴趣的:(springboot)