第一步:编写工具类,注意openSession 参数如增加true ,则为事务自动提交
package com.feijian.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;
//sqlSessionFactory
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//第一步,获取sqlSessionFactory 对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 有了sqlSessionFactory ,可以获得sqlSession实例,如果在opensession中增加true,意味着事务自动提交
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
}
第二步:编写核心配置文件 ,采用注释则注册的mapper 就是接口类了 class ,而不是resource
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://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=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.feijian.mapper.UserMapper"/>
</mappers>
</configuration>
第三步:编写接口类,并增加注解内容,实际上就是将之前UserMapper.xml的内容,写入到@Select @Update @Insert @Delete 等注释语句中
package com.feijian.mapper;
import com.feijian.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//获取全部用户
@Select("select * from mybatis.user;")
List<User> getUserList() ;
//根据ID查询用户
@Select("select * from mybatis.user where id = #{uid};")
User getUserById(@Param("uid") int id);
//insert一个新用户,影响的行数,所以返回boolean类型,true说明插入成功,一般不会反馈1表示插入成功!
@Insert("insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd})")
boolean addUser(User user);
//修改用户 修改的行数
@Update("update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};")
boolean updateUser(User user);
//删除用户
@Delete("delete from mybatis.user where id = #{id};")
boolean deleteUser(int id);
}
第四步:在核心配置文件中注册接口类 mapper ---已注册,注意是class
<mappers>
<mapper class="com.feijian.mapper.UserMapper"/>
</mappers>
第五步:调试, 一定要记住最后要关闭资源,close(), close(),close() 重要的事情强调三遍!!!
package com.feijian.mapper;
import com.feijian.pojo.User;
import com.feijian.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
//查询所有客户
@Test
public void test1(){
//获得sqlsession
SqlSession sqlSession = MybatisUtils.getSqlSession();
//找到UserMapper类并生成mapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用接口类中的方法,查询所有用户
List<User> userList = mapper.getUserList();
//遍历打印输出
for (User user : userList) {
System.out.println(user);
}
//关闭资源
sqlSession.close();
}
//根据ID查询
@Test
public void test2(){
//获得sqlsession
SqlSession sqlSession = MybatisUtils.getSqlSession();
//找到UserMapper类并生成mapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用接口类中的方法,查询所有用户
User user = mapper.getUserById(3);
//遍历打印输出
System.out.println(user);
//关闭资源
sqlSession.close();
}
//增加一个新用户
@Test
public void test3(){
//获得sqlsession
SqlSession sqlSession = MybatisUtils.getSqlSession();
//找到UserMapper类并生成mapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用接口类中的方法,查询所有用户
boolean flag =mapper.addUser(new User(6,"jun","230418"));
//遍历打印输出
System.out.println(flag);
System.out.println("增加新用户成功!");
//关闭资源
sqlSession.close();
}
//修改一个用户
@Test
public void test4(){
//获得sqlsession
SqlSession sqlSession = MybatisUtils.getSqlSession();
//找到UserMapper类并生成mapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用接口类中的方法,查询所有用户
boolean flag =mapper.updateUser(new User(5,"liu","1234567"));
//遍历打印输出
System.out.println(flag);
System.out.println("用户修改成功!");
//关闭资源
sqlSession.close();
}
//删除一个用户
@Test
public void test5(){
//获得sqlsession
SqlSession sqlSession = MybatisUtils.getSqlSession();
//找到UserMapper类并生成mapper对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用接口类中的方法,查询所有用户
boolean flag =mapper.deleteUser(9);
//遍历打印输出
System.out.println(flag);
System.out.println("用户删除成功!");
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
//关闭资源
sqlSession.close();
}
}
标签:mapper,04,18,UserMapper,sqlSession,user,2023,import,User
From: https://www.cnblogs.com/RUI2022/p/17328742.html