SpringBoot 从入门到入门(九 ORM-MyBatis)
程序员文章站
2022-03-08 15:39:11
...
一.增加 dependency
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
二.增加配置文件
在resources文件夹中新增 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>
<properties resource="properties.yml"/>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
其中,properties.yml同样位于resources文件夹中 存放的是一些数据库配置信息,也可以存放其他配置
driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: 123456
UserMapper.xml同样位于resources文件夹中,存放具体的查询信息,id为调用标识,resultType为返回数据类型
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="getUser" resultType="com.example.demo.entity.UserEntity">
select * from user
</select>
</mapper>
三.创建路由并访问
@RequestMapping("/users")
public List<UserEntity> users() throws IOException {
// mybatis-config.xml 为第二步中创建的文件
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
// getUser 为UserMapper.xml中定义好的查询id
return sqlSession.selectList("getUser");
}
四.带有参数的查询
编辑 UserMapper.xml,增加如下查询
<select id="getOneUser" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
创建路由并访问
@RequestMapping("/user")
public UserEntityuser(int id) throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
return sqlSession.selectOne("getOneUser",id);
}
五.如上方式在调用时,不够清晰明了,可以用另外一种方式调用
com.example.demo下创建mapper package,并创建UserMapper
package com.example.demo.mapper;
import com.example.demo.entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
List<UserEntity> getUser();
UserEntity getOneUser(@Param("id") int id);
}
创建路由并访问
@RequestMapping("/users1")
public List<UserEntity> users1() throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUser();
}
@RequestMapping("/user1")
public UserEntityuser1(int id) throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getOneUser(id);
}
六.除了之上mapper的引入方式,还有其他的引入方式