MyBits
1.1 Mybits安装
Maven
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
1.2 Mybits使用
1.2.1 配置Mybits连接数据库配置
相当于JDBC创建连接
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="131865"/>
</dataSource>
</environment>
<!-- <environment id="development2">-->
<!-- <transactionManager type="JDBC"/>-->
<!-- <dataSource type="POOLED">-->
<!-- <property name="driver" value="com.mysql.cj.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC"/>-->
<!-- <property name="username" value="root"/>-->
<!-- <property name="password" value="131865"/>-->
<!-- </dataSource>-->
<!-- </environment>-->
<!-- <environment id="">-->
<!-- ....-->
<!-- </environment>-->
</environments>
<mappers>
<mapper resource="org/example/DAO/UserMapper.xml"/>
</mappers>
</configuration>
1.2.2 创建SQLSessionFactory
工厂用于获取SqlSession
package org.example.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory=null;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
1.2.3 用户类
package org.example.mybatisDO;
public class User {
private int id;
private String name;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.2.4 UserMapper 接口
package org.example.DAO;
import org.example.mybatisDO.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
}
<?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="org.example.DAO.UserMapper">
<select id="getUserList" resultType="org.example.mybatisDO.User" >
select * from user;
</select>
<select id="getUserById" resultType="org.example.mybatisDO.User" parameterType="int">
select * from user where id=#{id};
</select>
</mapper>
1.2.5 测试
package org.example.DAO;
import org.apache.ibatis.session.SqlSession;
import org.example.MybatisUtils.MybatisUtils;
import org.example.mybatisDO.User;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User u:userList){
System.out.println(u.toString());
}
sqlSession.close();
}
@Test
public void testGetUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user.toString());
sqlSession.close();
}
}
1.3 常见问题
1、Type interface org.example.DAO.UserMapper is not known to the MapperRegistry.
在主配置mybatis-config.xml中没有注册Mapper
应在mybatis-config.xml中加入
<mappers>
<mapper resource="org/example/DAO/UserMapper.xml"/>
</mappers>
2、Maven因添加文件过滤,否则可能导致不符合约定的文件无法输出
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>