发布于2021-03-10 18:10 阅读(869) 评论(0) 点赞(23) 收藏(5)
<mapper namespace="com.perwrj.dao.UserMapper">
<!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” -->
<select id="getUserList" resultType="com.perwrj.pojo.User">
select * from mybatis.user
</select>
</mapper>
package com.perwrj.dao;
import java.util.List;
import com.perwrj.pojo.User;
public interface UserMapper {
// 查询全部用户
List<User> getUserList();
// 根据id查询用户
User getUserById(int id);
// 添加一个用户
int addUser(User user);
// 修改用户
int updateUser(User user);
// 删除用户
int deleteUser(int id);
}
<?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绑定一个对应的Mapper/Dao接口 -->
<mapper namespace="com.perwrj.dao.UserMapper">
<!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” -->
<select id="getUserList" resultType="com.perwrj.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.perwrj.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<!-- 对象中的属性可以直接取出来 -->
<insert id="addUser" parameterType="com.perwrj.pojo.User">
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="com.perwrj.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>
package com.perwrj.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.perwrj.pojo.User;
import com.perwrj.utils.MyBatisUtil;
public class UserMapperTest {
@Test
public void test() {
//获得SqlSession对象,
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
// 方式一:getMapper执行sql:更加简洁,类型更加安全,推荐使用
UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User>
userList = userMapper.getUserList();
for (User user : userList) { System.out.println(user); }
//方式一结束
// 方式二:老方法,不建议使用,
/*
* List<User> list =
* sqlSession.selectList("com.perwrj.dao.UserMapper.getUserList");
*
* for (User object : list) { System.out.println(object); }
*/
//方式二结束
}catch(Throwable e) {
e.printStackTrace();
}finally {
// 关闭sqlSession
sqlSession.close();
}
}
/**
* 删除操作
*/
@Test
public void deleteUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.deleteUser(4);
if(num > 0) {
// 提交事务
System.out.println(num);//1
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
/**
* 改操作
*/
@Test
public void updateUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.updateUser(new User(2,"杰瑞","987654"));
if(num > 0) {
// 提交事务
System.out.println(num);//1
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
/**
* 增删改需要提交事务
*/
@Test
public void addUserTest() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int num = userMapper.addUser(new User(4,"王瑞杰","123568"));
if(num > 0) {
// 提交事务
System.out.println(num);//1
sqlSession.commit();
sqlSession.close();
}
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void testGetUserById() {
//获得SqlSession对象,
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
}catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
作者:咿呀咿呀哟
链接:http://www.javaheidong.com/blog/article/112345/24d04c380eb5a28c66a6/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!