SpringBoot配置mybatis有两种方式,一种为注解方式,一种为传统的用xml文件来存sql语句!下面分别介绍这两种方式!
一:注解方式(copy网上)
这种方式只适用于简单sql查询的场景,对于复杂sql的场景,我们还是希望将sql写在xml文件中的
1创建User类
- package com.woniu.bean;
- public class User {
- private long id;
- private String name;
- private int age;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
- }
- }
2创建UserMapper接口
创建接口UserMapper,并添加@Mapper注解
- package com.woniu.mapper;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
- import com.woniu.bean.User;
- @Mapper
- public interface UserMaper {
- @Select("select * from user where age = #{age}")
- User Select(int age);
- }
3创建controller
- package com.woniu.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.woniu.bean.User;
- import com.woniu.mapper.UserMaper;
- @RestController
- @RequestMapping("/web")
- public class WebController {
- @Autowired
- private UserMaper mapper;
- @RequestMapping("/index")
- public User selectAge(int age){
- return mapper.Select(age);
- }
- }
4设置application.properties
- # mysql
- spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&characterEncoding=utf-8
- spring.datasource.username=root
- spring.datasource.password=123456
- 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