spring+mybatis 修改删除和查询
1、在UserMapper接口类中,创建接口
在UserMapper接口类中,创建接口
package com.wjw.mybatis.mapper;
import com.wjw.mybatis.pojo.User;
import java.util.List;
/**
* @Author wjwdive
* @Date 2023/2/19 22:54
* @Description:
*/
public interface UserMapper {
/**
* 添加用户信息
* @return
*/
int insertUser();
/**
* 修改用户系信息
*/
void updateUser();
/**
* 删除用户
*/
void deleteUser();
/**
* 根据id查询用户信息
* @return
*/
User getUserById();
/**
* 获取所欲用户信息
* @return
*/
List<User> getAllUsers();
}
2、对应的要在mybatis-config.xml配置文件 中配置接口对应的SQL查询语句
<?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.wjw.mybatis.mapper.UserMapper">
<!--
mypper接口和映射文件要保持两个一致:
1、mapper接口的全类名和映射文件的namespace一致
2、mapper接口中的方法名要和映射文件中的sql的id一致
-->
<!-- int insertUser(); -->
<insert id="insertUser">
insert into t_user values(null,'admin','123456',23,'男','[email protected]')
</insert>
<!-- void updateUser();-->
<update id="updateUser">
update t_user set username = 'Karvis', password = "0000" where id="0000000001"
</update>
<!-- void deleteUser();-->
<delete id="deleteUser">
delete from t_user where id = 0000000002
</delete>
<!-- User getUserById();
resultType:设置结果类型,即查询的数据要转换为的java类型 字段名和属性名一致就用这个 不是User,而是唯一标识
resultMap:自定义映射,处理多对一或一对多的映射关系 字段名和属性名需要映射,就用这个
-->
<select id="getUserById" resultType="com.wjw.mybatis.pojo.User">
select * from t_user where id = 0000000003
</select>
<!-- List<User> getAllUsers();
resultType 还是pojo类的唯一标识
-->
<select id="getAllUsers" resultType="com.wjw.mybatis.pojo.User">
select * from t_user
</select>
</mapper>
3、在测试类中写对应的测试方法
package com.wjw.mybatis.test;
import com.wjw.mybatis.mapper.UserMapper;
import com.wjw.mybatis.pojo.User;
import com.wjw.mybatis.utils.SqlSessionUtil;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @Author wjwdive
* @Date 2023/2/19 23:32
* @Description:
*/
public class MyBatisTest {
@Test
public void testInsert() throws IOException {
//获取核心配置文件的输入流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取sql的会话对象SqlSession,是mybatis提供的操作数据库对象的方法
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动提交事务,需设置参数 为true
//获取UserMapper的代理实现类对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//调用 mapper接口中的方法,实现添加用户信息的功能
int result = mapper.insertUser();
//提交事务【不提交事务,会自动回滚执行的脚本】
sqlSession.commit();
System.out.println("结果:"+result);
//关闭连接
sqlSession.close();
}
@Test
public void update() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser();
sqlSession.close();
}
@Test
public void testDelete() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser();
sqlSession.close();
}
@Test
public void testGetUserById() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById();
System.out.println("user: "+user);
sqlSession.close();
}
@Test
public void testGetAllUsers() {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> list = mapper.getAllUsers();
list.forEach(System.out::println);
sqlSession.close();
}
}
其中,SqlSessionUtil 是抽取的一个获取sqlSession对象的一个工具类方法
放在了 com.wjw.mybatis.utils包下,
package com.wjw.mybatis.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;
/**
* @Author wjwdive
* @Date 2023/2/22 20:01
* @Description:
*/
public class SqlSessionUtil {
public static SqlSession getSqlSession() {
SqlSession sqlSession = null;
try {
//获取核心配置文件的输入流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取SqlSession对象
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
}
标签:mapper,spring,UserMapper,SqlSession,ssm,sqlSession,23001,mybatis,import
From: https://www.cnblogs.com/wjw-blog/p/17146107.html