查询记录
<select id="getUserList" resultType="org.example.pojo.User">
select * from user.use1
</select>
<select id="getUserByid" resultType="org.example.pojo.User" parameterType="int">
select * from user.use1 where id = #{id};
</select>
增加
//插入
int AddUser(User user);
<!-- 对象中的属性,可以直接取出来-->
<insert id="AddUser" parameterType="org.example.pojo.User">
insert into user.use1(id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
修改
// 修改
int UpdateUser(User user);
<update id="UpdateUser" parameterType="org.example.pojo.User">
update User.use1 set name=#{name},pwd=#{pwd} where id=#{id};
</update>
删除
// 删除
int DeleteUser(User user);
<delete id="DeleteUser" parameterType="org.example.pojo.User">
delete from User.use1 where id=#{id};
</delete>
测试类
package org.example.dao;
import org.apache.ibatis.session.SqlSession;
import org.example.Mapper.UserDao;
import org.example.pojo.User;
import org.example.util.MybatisUtil;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
//获得SqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession();
//执行sql
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserByid(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = mapper.getUserByid(1);
System.out.println(user);
sqlSession.close();
}
//增删改需要提交事务
@Test
public void AddUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
int popo = mapper.AddUser(new User(5, "opo", "232"));
if(popo>=1){
System.out.println("增加成功");
}
// 提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void UpdateUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
int popo = mapper.UpdateUser(new User(5, "opo", "232111"));
if(popo>=1){
System.out.println("修改成功");
}
// 提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void DeleteUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
int popo = mapper.DeleteUser(new User(5, "opo", "232111"));
if(popo>=1){
System.out.println("删除成功");
}
// 提交事务
sqlSession.commit();
sqlSession.close();
}
}
标签:mapper,UserDao,sqlSession,---,CURD,User,mybatis,id,user
From: https://www.cnblogs.com/solutide/p/17859862.html