二、spring boot配置mybatis
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
然后如果如果想使用mybatis需要对mybatis进行配置,我们知道mybatis的使用方式有两种,一种是使用注解的方式写sql语句
@Select("SELECT * FROM user WHERE name = #{name}")
User findByName(@Param("name") String name);
@Results({
@Result(property = "name", column = "name"),
@Result(property = "age", column = "age")
})
@Select("SELECT name, age FROM user")
List<User> findAll();
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
int insert(@Param("name") String name, @Param("age") Integer age);
另外还有一种是使用mapper文件,每一个dao层只写接口不写实现,然后对应写一个mapper.xml文件。
注解版
首先新建一个测试数据库,tb_user有id、username、password三个字段,自己伪造几条数据,这里只是将spring boot和mybatis跑通,就是简单的查询。
pojo中写一个user实体类
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
dao中写mapper接口
public interface TbUserMapper {
@Select("SELECT * FROM tb_user WHERE username = #{username}")
User findByName(@Param("username") String username);
}
加扫描包
写一个test类
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private TbUserMapper tbUserMapper;
@Test
public void testQuery() throws Exception {
User users = tbUserMapper.findByName("tidy");
System.out.println(users.getPassword());
}
}
运行test结果
到此注解的方式就完成了
xml形式
修改application.properties
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.shop.pojo
spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
修改TbUserMapper
public interface TbUserMapper {
User findByName(@Param("username") String username);
}
添加配置文件
添加配置文件TbUserMapper.xml和mybatis-config.xml.目录同application.properties配置目录相同。
mybatis-config.xml 这与SSM配置相同,也可配置其他内容,如分页插件
<?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>
<!-- 配置分页插件 -->
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
mybatis-config.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.shop.mapper.TbUserMapper" >
<resultMap id="user" type="com.shop.pojo.User" >
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<select id="findByName" resultMap="user" >
SELECT username,password FROM tb_user WHERE username = #{username}
</select>
</mapper>
其他内容不变,运行测试类结果相同
xml形式完成后项目目录如下: