Mybatis框架搭建步骤
一、创建一个Maven项目
二、在pom.xml文件中配置相关jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xk</groupId>
<artifactId>mybatis-01</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-1-base</module>
<module>mybatis-2</module>
</modules>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</project>
三、搭建好框架
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/21660c39241e43c91c63b61d524ac48d.png)
四、编写mybatis核心配置文件
1. 连接数据库:
① 数据库连接参数文件
username=root
password=123456
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
driver=com.mysql.cj.jdbc.Driver
② 配置properties
<properties resource="db.properties"></properties>
③ environments
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<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>
五、写Mybatis的工具类
public class MybatisUtils
{
static SqlSessionFactory sqlSessionFactory;
static
{
String resource="mybatis-conf.xml";
try {
InputStream resourceAsStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public SqlSession getSession()
{
return sqlSessionFactory.openSession();
}
}
六、实体类
public class User
{
private Integer id;
private String name;
public User() {
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
七、编写Mapper接口
public interface UserMapper
{
public List<User> selAll();
}
八、mapper.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.xk.mapper.UserMapper">
<select id="selAll" resultType="com.xk.pojo.User">
select * from user
</select>
</mapper>
九、在mybatis核心配置文件中注册mapper.xml
<mappers>
<mapper resource="userMapper.xml"></mapper>
</mappers>
十、测试
public static void main(String[] args)
{
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.selAll();
if(users!=null)
{
for(User user:users)
{
System.out.println(user);
}
}
sqlSession.close();
}