1.MAVEN依赖
mybatis为了配合Springboot,提供了一个新的依赖包,我们在springboot项目中引入下面这个依赖。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency>
2.配置文件
相比于单独的使用Mybatis,在Spring Boot中只增加了一步配置,指定Myabtis配置文件路径:
mybatis.config-location=classpath:mybatis/mybatis-config.xml
然后我们在Mybatis的配置文件中配置的属性与之前无异
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--禁用缓存--> <setting name="cacheEnabled" value="false"/> <!--下划线转换为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--自动生成主键。我这里使用oracle数据库,因此不支持该操作,如果是mysql或sql Server可以设置为true--> <setting name="useGeneratedKeys" value="false"/> <!--在嵌套语句中使用行分界,这里设为false--> <setting name="safeRowBoundsEnabled" value="false"/> <!--配置默认的执行器--> <setting name="defaultExecutorType" value="REUSE"/> <!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 --> <setting name="defaultStatementTimeout" value="600"/> </settings> <typeAliases> <typeAlias type="com.springboot.demo.model.User" alias="user"/> </typeAliases> <mappers> <mapper resource="mapper/user.xml" /> </mappers> </configuration>
3.创建程序
配置文件指定好了之后,我们编写相关的Mapper接口和配置文件:
这里配置两个查询方法和一个新增方法,一个是根据id来进行唯一查询,一个是根据User来进行匹配查询,还有一个是新增用户。
@Mapper public interface UserMapper { User findById(long id); List<User> findList(User user);
long insert(User user); }
下面的user.xml用来存放接口对应的方法和sql语句
<?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.springboot.demo.mapper.UserMapper"> <sql id="userFields"> id, name, password, email, age, sex </sql> <select id="findById" resultType="user"> select <include refid="userFields" /> from d_user where id = #{id} </select> <select id="findList" resultType="user"> select <include refid="userFields" /> from d_user <where> <if test="id != null and id != 0"> and id = #{id} </if> <if test="name != null and name != ''"> and name = #{name} </if> <if test="password != null and password != ''"> and password = #{password} </if> <if test="email != null and email != ''"> and email = #{email} </if> <if test="age != null and age != 0"> and age = #{age} </if> <if test="sex != null and sex != ''"> and sex = #{sex} </if> </where> </select> <insert id="insert"> insert into d_user (<include refid="userFields"/>) VALUES ( #{id}, #{name}, #{password}, #{email}, #{age}, #{sex} ) </insert> </mapper>
4.编写测试代码
4.1新增用户
我们先添加几个用户进去(由于用的是oracle数据库,不支持自增主键,因此采用简单的手工输入的方式)
@Test public void insert() { User user = new User(); user.setId(1112); user.setName("zs"); user.setPassword("111"); user.setEmail("2@qq.com"); user.setAge(13); user.setSex("男"); long i = mapper.insert(user); System.out.println("i=[" + i + "]"); //插入成功返回1,表示成功插入1条数据. }
4.2根据ID查询用户
@Test public void findById() { User user = mapper.findById(1112); System.out.println("USER=[" + user.toString() + "]"); }
4.3根据User信息查询用户
@Test public void find() { User user = new User(); user.setSex("男"); List<User> list = mapper.findList(user); System.out.println("LIST>SIZE=" + list.size()); list.forEach((user1) -> { System.out.println("USER=[" + user.toString() + "]"); }); }