SpringBoot整合mybatis

SpringBoot配置mybatis有两种方式,一种为注解方式,一种为传统的用xml文件来存sql语句!下面分别介绍这两种方式!

一:注解方式(copy网上)

这种方式只适用于简单sql查询的场景,对于复杂sql的场景,我们还是希望将sql写在xml文件中的

1创建User类

[html]  view plain  copy
  1. package com.woniu.bean;  
  2.   
  3.   
  4. public class User {  
  5.     private long id;  
  6.     private String name;  
  7.     private int age;  
  8.     public long getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(long id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public int getAge() {  
  21.         return age;  
  22.     }  
  23.     public void setAge(int age) {  
  24.         this.age = age;  
  25.     }  
  26.     @Override  
  27.     public String toString() {  
  28.         return "User [id=" + id + "name=" + name + "age=" + age + "]";  
  29.     }  
  30.       
  31.       
  32. }  

2创建UserMapper接口

      创建接口UserMapper,并添加@Mapper注解
[html]  view plain  copy
  1. package com.woniu.mapper;  
  2.   
  3. import org.apache.ibatis.annotations.Mapper;  
  4. import org.apache.ibatis.annotations.Select;  
  5.   
  6. import com.woniu.bean.User;  
  7.   
  8. @Mapper  
  9. public interface UserMaper {  
  10.       
  11.     @Select("select * from user where age = #{age}")  
  12.     User Select(int age);  
  13. }  

3创建controller

[html]  view plain  copy
  1. package com.woniu.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. import com.woniu.bean.User;  
  8. import com.woniu.mapper.UserMaper;  
  9.   
  10. @RestController  
  11. @RequestMapping("/web")  
  12. public class WebController {  
  13.     @Autowired  
  14.     private UserMaper mapper;  
  15.     
  16.     @RequestMapping("/index")  
  17.     public User selectAge(int age){  
  18.           
  19.         return mapper.Select(age);  
  20.     }  
  21. }  

4设置application.properties

[html]  view plain  copy
  1. # mysql  
  2. spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&characterEncoding=utf-8  
  3. spring.datasource.username=root  
  4. spring.datasource.password=123456  
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver  

二:xml配置方式

项目结构如下:


首先在application.yml配置mybatis

# mybatis配置
mybatis:
  mapper-locations: classpath:mybatis/*.xml

然后再应用的主类上配置如下,加上@MapperScan:

@SpringBootApplication(scanBasePackages = { "com.flysun" })
@EnableDiscoveryClient
@EnableCircuitBreaker
@MapperScan("com.flysun.mapper")
@EnableApolloConfig
public class Application {

然后写mapper接口:TestMapper.java

@Repository("testMapper")
public interface TestMapper {

    /**
     * 根据条件分页查询流程列表
     */
    List<ProcessDTO> queryProcessList(ProcessDTO processDTO);

}

然后写mapper的配置文件:TestMapper.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.flysun.mapper.TestMapper">

    <resultMap id="BaseProcessResult" type="com.flysun.dto.ProcessDTO">
        <result column="id" property="id"></result>
        <result column="process_name" property="processName"></result>
    </resultMap>

    <sql id="queryProcessCondition">
        <where>
            isactive = 1
            <if test="processId != null and processId != ''">
                AND process_id = #{processId}
            </if>
            <if test="matchProcessName != null and matchProcessName != ''">
                AND process_name LIKE concat('%',#{matchProcessName},'%')
            </if>
        </where>
    </sql>

    <select id="queryProcessList" parameterType="com.flysun.dto.ProcessDTO" resultMap="BaseProcessResult">
        SELECT id,process_id,process_name
        FROM table_process
        <include refid="queryProcessCondition"></include>
    </select>

</mapper>

然后就是在service调用mapper

public class TestServiceImpl implements TestService {

    @Resource
    private TestMapper testMapper;

    @Override
    public List<ProcessDTO> queryProcessList(ProcessDTO processDTO) {
        
               testMapper.queryProcessList(processDTO);
            }
}

大工告成


或者参考博客:

http://www.mooooc.com/springboot/2016/11/06/springboot(六)-如何优雅的使用mybatis.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值