在普通Maven项目中引入Mybatis
1、引入依赖包
- 打开项目根目录下的pom.xml
- 对dependencies进行配置
- 即引入mybatis和mysql-connector-java两个依赖包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
2、编写mybatis核心配置文件
在项目中的资源文件夹resources下
创建mybatis的核心配置文件mybatis-config.xml
关键在配置数据源dataSource、以及mapper.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="888888"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/yao/dao/UserMapper.xml"/>
</mappers>
</configuration>
3、将获取sqlSession对象的过程进行封装
因为获取sqlSession需要
- 用Resources类下的getResourceAsStream方法将mybatis配置文件mybatis-config.xml加载为流
- 再使用SqlSessionFactoryBuilder中的build方法将流加载进去,获取SqlSessionFactory对象
- 使用SqlSessionFactory来获取sqlSession对象
每次使用sqlSession都需要重复这样的操作,所以可以将这个过程封装
package com.yao.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import java.io.IOException;
import java.io.InputStream;
//sqlSessionFactory -> sqlSession
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() {
return sqlSessionFactory.openSession();
}
}
4、创建实体类
package com.yao.pojo;
//实体类
public class User {
private int id;
private String name;
private String pwd;
//构造方法和get、set方法
}
5、创建Mapper接口以及对应的配置文件
Mapper接口
package com.yao.dao;
import com.yao.pojo.User;
import java.util.List;
public interface UserMapper {
//查询所有用户
List<User> getUserList();
//根据ID查找用户
User getUserById(int id);
//添加一个用户
int addUser(User user);
//更改一个用户
int updateUser(User user);
//删除一个用户
int deleteUser(int id);
}
Mapper对应的配置文件
该配置文件路径需要在mybatis核心配置文件mybatis-config.xml中的mapper标签中配置
<?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.yao.dao.UserMapper">
<select id="getUserList" resultType="com.yao.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" resultType="com.yao.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id}
</select>
<insert id="addUser" parameterType="com.yao.pojo.User" >
insert into mybatis.user (id, name, pwd) value (#{id}, #{name}, #{pwd})
</insert>
<update id="updateUser" parameterType="com.yao.pojo.User" >
update mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id} ;
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id} ;
</delete>
</mapper>
6、测试
配置就基本完成了,可以开始进行测试
获取sqlSession对象—>获取mapper对象–>执行mapper中的方法
package com.yao.dao;
import com.yao.pojo.User;
import com.yao.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class TestDao {
@Test
public void test(){
//获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//getMapper
try{
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally{
sqlSession.close();
}
}
@Test
public void testUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void testInsertUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4, "haha", "123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4, "hahage78", "123456"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}