1,首先创建一个数据库和一个表
2,创建一个maven父工程,删掉里面的src文件在pom.xml文件中添加
dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xproperties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml/</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
3(编写MyBatis核心配置文件),mybatis-config.xml文件里面的内容
<?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:核心配置文件-->
<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&Unicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="010616"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml文件都需要在mybatis核心配置文件中注册-->
<!--这个Mapper.xml文件也是包名+/+UserMapper.xml,同理包名里面的.要换成/ -->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
4(编写MyBatis工具类),创建一个utils包,里面添加一个MybatisUtils工具类,先定义一个静态的sqlSessionFactory: private static SqlSessionFactory sqlSessionFactory
再定义一个静态代码快(可以首先执行)里面写mybatis官网里面给出的三行代码:
String resource = "mybatis-config.xml"(如果在包下面创建,则需要写包名+/mybatis-config.xml。包名里面的.都要换成/);
InputStream inputStream =Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
之后抛出异常用try catch来捕获
最后我们既然有了sqlSessionFactory对象,那么我们就可以获得SqlSession实例(通过sqlSessionFactory对象的openSession()方法来获得sqlSession
public satatic SqlSession getSqlSession(){
SqlSession sqlSession=sqlSessionFactory.openSession();
return sqlSession
}
如下:
package Utils;
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;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getsqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
5(创建实体类),然后添加一个子项目,里面创建一个pojo实体类,实体类里面写一个class类User,User里面定义的参数于数据库里面的参数一致。重写他的get,set方法toString方法以及无参有参的构造方法。
6(编写Mapper接口类),:创建com.chen.dao包,包里面添加一个UserDao接口,接口里面写Lsit<>集合,<>里面的参数就是实体类(因为这个接口就是操作数据库对象的实体),List后面跟一个方法getUserList()
7(编写UserMapper.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">
<!--namespace:绑定一个对应的UsaerDao、UserMapper接口-->
<mapper namespace="com.chen.dao.UserDao">
<!--id相当于UserDao接口中方法的名字-->
<!-- resultType返回结果类型局势UserDao接口中的List<User>(里面就一个User-->
<select id="getUserList" resultType="com.chen.pojo.User">
select * from mybatis.user
</select>
</mapper>
8,test测试类里面的内容
import com.chen.dao.UserDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MybatisUtils;
import java.util.List;
public class MyTest {
@Test
public void run(){
// 获得Sqlsession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 执行sql
UserDao userdao = sqlSession.getMapper(UserDao.class);
List<User> userList = userdao.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
}