美文网首页
A.7 springboot mybatis

A.7 springboot mybatis

作者: 成长的键盘手 | 来源:发表于2018-02-12 00:34 被阅读0次

    springboot mybatis

    1. 工程搭建

    依据第一章节的样例工程,进行更改。

    1.1 pom修改

    需要引入:

    • mysql jdbc 驱动包
    • mybatis-spring-boot-starter(整合MyBatis的核心依赖)

    详细内容如下:

    <!-- mybatis & db -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    

    1.2 application.properties 修改

    • 增加数据库连接
    • mybatis mapper文件扫描位置
    #jdbc config
    spring.datasource.url=jdbc:mysql://192.168.137.101:3306/spring-boot-demo?characterEncoding=utf-8&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.max-active=5
    spring.datasource.max-idle=3
    spring.datasource.test-on-borrow=true
    spring.datasource.test-while-idle=true
    spring.datasource.validation-query=SELECT 1;
    
    #mybatis config
    mybatis.mapperLocations = classpath:mapper/*.xml
    mybatis.typeAliasesPackage = pers.mateng.demo.springboot
    

    2. 业务编码

    2.1 创建pojo

    package pers.mateng.demo.springboot.domain;
    
    public class User {
        
        private Long id;
        
        private String name;
        
        private Integer age;
        
        get/set ...
    }
    
    package pers.mateng.demo.springboot.dto;
    
    public class UserCondition {
    
        /**用户名*/
        private String name;
        
        /**根据年龄范围查询用户,范围的最小值*/
        private Integer minAge;
        
        /**根据年龄范围查询用户,范围的最大值*/
        private Integer maxAge;
        
        /**分页条件,起始位置*/
        private Integer startPosition;
        
        /**分页条件,查询的最大条数*/
        private Integer maxResult;
    }
    

    2.2 创建DAO

    interface

    @Mapper
    public interface UserDao {
        
        public int add(User user);
        
        public int delete(Long id);
        
        public User getById(Long id);
        
        public List<User> getList(UserCondition condition);
        
        public Long getCount(UserCondition condition);
    
    }
    

    mapper

    <?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" > 
    <?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="pers.mateng.demo.springboot.dao.UserDao" >
    
        <resultMap id="user" type="pers.mateng.demo.springboot.domain.User">
            <id column="u_id" property="id" />
            <result column="u_name" property="name" />
            <result column="u_age" property="age" />
        </resultMap>
          
        <select id="getById" resultMap="user">  
            select * from tb_user where u_id = #{id}  
        </select>
          
        <select id="getList" resultMap="user" parameterType="pers.mateng.demo.springboot.dto.UserCondition">  
            select * from tb_user 
            <include refid="where"></include>
            limit #{startPosition}, #{maxResult}
        </select> 
        
        <select id="getCount" parameterType="pers.mateng.demo.springboot.dto.UserCondition" resultType="Long">  
            select count(1) from tb_user 
            <include refid="where"></include>
        </select>
        
        <sql id="where">
            <where>
                <if test="name != null and name.length() > 0">
                    and u_name LIKE CONCAT('%', #{name}, '%')
                </if>
                <if test="minAge != null">
                    and u_age <![CDATA[ >= ]]> #{minAge}
                </if>
                <if test="maxAge != null">
                    and u_age <![CDATA[ <= ]]> #{maxAge}
                </if>
            </where>
        </sql>
          
        <insert id="add" parameterType="pers.mateng.demo.springboot.domain.User">  
            insert into tb_user(u_name, u_age) values(#{name,jdbcType=VARCHAR}, #{age,jdbcType=TINYINT})
        </insert>  
          
        <delete id="delete">  
            delete from tb_user where u_id = #{id}  
        </delete>  
        
    </mapper>  
    

    2.3 controller

    /**
     * 用户管理的controller
     * @author mateng
     */
    @RestController
    @RequestMapping(path="user")
    public class UserController {
        
        @Autowired
        private UserDao userDao;
        
        @RequestMapping(method=RequestMethod.GET)
        public Map<String, Object> page(@ModelAttribute UserCondition condition) {
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("total", userDao.getCount(condition));
            result.put("rows", userDao.getList(condition));
            return result;
        }
        
        @RequestMapping(method=RequestMethod.POST)
        public int add(@ModelAttribute User user) {
            return userDao.add(user);
        }
        
        @RequestMapping(path="/{id}", method=RequestMethod.GET)
        public User findById(@PathVariable Long id) {
            return userDao.getById(id);
        }
        
        @RequestMapping(path="/{id}", method=RequestMethod.DELETE)
        public int delete(@PathVariable Long id) {
            return userDao.delete(id);
        }
    
    }
    

    3. 验证

    启动工程,使用如下命令测试增加、查询接口。注意:下面连接中的ip地址(当前开发机器的ip地址)

    1、增加:

    curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' -d "name=zhansan&age=30" 'http://192.168.50.7:8888/user'

    2、查询:

    分页查询

    curl -X GET 'http://192.168.50.7:8888/user'

    根据id查询

    curl -X GET 'http://192.168.50.7:8888/user/1'

    5. 源码

    springboot-demo-5

    相关文章

      网友评论

          本文标题:A.7 springboot mybatis

          本文链接:https://www.haomeiwen.com/subject/iotrtftx.html