Mybatis入门实现CURD
2021-10-14 本文已影响0人
zzqsmile
Mapper字段笔记
- namespace
namespace的包名要和UserMapper/UserDao接口报名一致
- id
id就是namespace中对应的方法名
- resultType
resultType是SQL语句返回值
- parameterType
参数类型
编写步骤
- 编写接口
UserMapper/UserDao
:
- 编写接口
package com.zzqsmile.dao;
import com.zzqsmile.pojo.User;
import java.util.List;
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserByID(int id);
//增加用户
int userAdd(User user);
//修改用户
int userUpd(User user);
//删除用户
int userDel(int id);
}
- 编写对应的Mapper中的sql语句
UserMapper.xml
:
- 编写对应的Mapper中的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">
<!--namespace绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.zzqsmile.dao.UserMapper">
<!--select查询-->
<select id="getUserList" resultType="com.zzqsmile.pojo.User">
select * from mybatis.user
</select>
<select id="getUserByID" resultType="com.zzqsmile.pojo.User" parameterType="int">
select * from mybatis.user where id = #{id}
</select>
<insert id="userAdd" parameterType="com.zzqsmile.pojo.User">
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="userUpd" parameterType="com.zzqsmile.pojo.User">
update mybatis.user set name = #{name},pwd = #{pwd} where id = #{id}
</update>
<delete id="userDel" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
- 测试
package com.zzqsmile.dao;
import com.zzqsmile.pojo.User;
import com.zzqsmile.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void Test(){
//第一步:获取SqlSesson对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//第二步:执行SQL
//方式1:getMapper推荐使用
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
//方式2:
//List<User> userList = sqlSession.selectList("com.zzqsmile.dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User userByID = mapper.getUserByID(1);
System.out.println(userByID);
}
//增删改需要提交事物
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.userAdd(new User(4, "zhangsan", "111111"));
if (res>0){
System.out.println("添加成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void userUpd(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res1 = mapper.userUpd(new User(4, "zhangsan", "123123"));
if (res1>0){
System.out.println("修改成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void userDel(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res2 = mapper.userDel(4);
if (res2>0){
System.out.println("删除成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
}
注意,增删改需要提交事物才能生效
万能Map
- UserMapper:
//万能Map
int userAdd2(Map<String,Object> map);
- UserMapper.xml
<insert id="userAdd2" parameterType="map">
insert into mybatis.user (id,pwd) values (#{id},#{userpwd})
</insert>
- UserDaoTest
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> stringObjectHashMap = new HashMap<String, Object>();
stringObjectHashMap.put("id",5);
stringObjectHashMap.put("userpwd","222222");
mapper.userAdd2(stringObjectHashMap);
//提交事务
sqlSession.commit();
sqlSession.close();
}