SQL 和业务代码应该隔离,方便和校对 SQL.
XML 对较长的 SQL 比较清晰。
pom.xml中注入依赖
<!-- spring boot核心模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- spring boot测试模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- spring boot Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql链接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
创建简单java类
package com.didispace.domain;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
mybaits所需接口
package com.didispace.dao.mapper;
import com.didispace.domain.User;
public interface UserMapper {
User findByFilter(User user);
}
接口对应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.didispace.dao.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.didispace.domain.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<select id="findByFilter" resultMap="BaseResultMap" parameterType="com.didispace.domain.User">
SELECT *
FROM user
<where>
<if test="name !=null">
name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
</mapper>
测试用控制层
package com.didispace.action;
import com.didispace.dao.mapper.UserMapper;
import com.didispace.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
public class Action {
@Autowired
private UserMapper userMapper;
@RequestMapping("/select")
public User selectUser(@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "age",required = false)Integer age){
User user = new User();
user.setName(name);
user.setAge(age);
return userMapper.findByFilter(user);
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
application运行主程序
package com.didispace;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// mapper 接口类扫描包配置
@MapperScan("com.didispace.dao.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}