首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
第一章:注解开发介绍
第二章:环境准备
第三章:注解实现常用的 CRUD
第四章:注解实现一对一关联查询
第五章:注解实现一对多关联查询
第六章:注解实现多对多关联查询
第七章:分页插件 -- PageHelper
第八章:批量操作
第九章:自定义类型处理器
第十章:SQL 构建对象介绍
第十一章:准备工作
第十二章:使用 SQL 功能类构建 SQL 语句
第十三章:查询功能的实现
第十四章:新增功能的实现
第十五章:更新功能的实现
第十六章:删除功能的实现
当前位置:
首页>>
技术小册>>
Mybatis合辑5-注解、扩展、SQL构建
小册名称:Mybatis合辑5-注解、扩展、SQL构建
- User.java ``` package com.sunxiaping.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private Integer id; private String username; private Date birthday; private String gender; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", gender='" + gender + '\'' + ", address='" + address + '\'' + '}'; } } ``` - UserMapper.java ``` package com.sunxiaping.mapper; import com.sunxiaping.domain.User; import org.apache.ibatis.annotations.*; import java.util.List; public interface UserMapper { /** * 查询所有用户信息 * * @return */ @Select(" SELECT * FROM `user`") @Results({ @Result(id = true, property = "id", column = "id"), @Result(property = "username", column = "username"), @Result(property = "birthday", column = "birthday"), @Result(property = "gender", column = "gender"), @Result(property = "address", column = "address"), }) List<User> findAll(); /** * 添加用户信息 * * @param user */ @Insert(" INSERT INTO `user` (username,birthday,gender,address) VALUES (#{username},#{birthday},#{gender},#{address})") void saveUser(User user); /** * 修改用户信息 * * @param user */ @Update(" UPDATE `user` set username = #{username},gender= #{gender} ,address = #{address} WHERE id = #{id} ") void updateUser(User user); /** * 删除用户信息 * * @param id */ @Delete(" DELETE FROM `user` WHERE id = #{id} ") void deleteById(Integer id); } ``` - 测试: ``` package com.sunxiaping; import com.sunxiaping.domain.User; import com.sunxiaping.mapper.UserMapper; 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.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; public class UserTest { SqlSessionFactory sqlSessionFactory = null; SqlSession sqlSession = null; @Before public void before() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @After public void after() { if (sqlSession != null) { sqlSession.close(); } } @Test public void testFindAll() throws IOException { sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.findAll(); System.out.println("userList = " + userList); } @Test public void testSaveUser() { sqlSession = sqlSessionFactory.openSession(true); User user = new User(); user.setUsername("zhangsan"); user.setBirthday(new Date()); user.setGender("男"); user.setAddress("江苏苏州"); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.saveUser(user); } @Test public void testUpdateUser() { sqlSession = sqlSessionFactory.openSession(true); User user = new User(); user.setId(1); user.setUsername("张三"); user.setGender("女"); user.setAddress("江苏苏州"); user.setBirthday(new Date()); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.updateUser(user); } @Test public void testDeleteById() { sqlSession = sqlSessionFactory.openSession(true); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.deleteById(1); } } ```
上一篇:
第二章:环境准备
下一篇:
第四章:注解实现一对一关联查询
该分类下的相关小册推荐:
Mybatis合辑4-Mybatis缓存机制
Java语言基础15-单元测试和日志技术
Java必知必会-Maven初级
Java语言基础12-网络编程
Java语言基础5-面向对象初级
Java高并发秒杀入门与实战
Java语言基础16-JDK8 新特性
Mybatis合辑3-Mybatis动态SQL
Java语言基础13-类的加载和反射
Java语言基础6-面向对象高级
Java语言基础2-运算符
Mybatis合辑2-Mybatis映射文件