MyBatis实现基于Mapper接口代理Dao的CURD

网友投稿 548 2022-10-18

MyBatis实现基于Mapper接口代理Dao的CURD

MyBatis实现基于Mapper接口代理Dao的CURD

1.User类

package com.domain;import java.io.Serializable;import java.util.Date;public class User implements Serializable{ private Integer id; private String username; private String address; private String sex; private Date birthday; 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", address='" + address + '\'' + ", sex='" + sex + '\'' + ", birthday=" + birthday + '}'; }}

2.SqlMapConfig.xml主配置文件

3.IUserDao接口

package com.dao;import com.domain.QueryVo;import com.domain.User;import java.util.List;//用户的持久层接口public interface IUserDao { //查询所有 List findall(); //根据id查询 User findbyid(Integer id); /** *保存用户 * @param user * @return 影响数据库记录的行数 */ int saveuser(User user); /** * 更新用户 * @param user * @return */ int updateUser(User user); /** * 根据id删除用户 * @param id * @return */ int deleteUser(Integer id); /** * 根据名称模糊查询 * @param username * @return */ List finguserbyname(String username); /** * 查询总记录数 * @return */ int findtotal(); /** * 根据QueryVo中的条件查询用户 * @param vo * @return */ List findbyvo(QueryVo vo);}

4.IUserDao.xml 用户的配置文件

select last_insert_id(); insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}); update user set username=#{username},birthday=#{birthday},sex=#{sex}, address=#{address} where id=#{id} delete from user where id = #{id};

5测试类MybatisTest

package com.test;import com.dao.IUserDao;import com.domain.QueryVo;import com.domain.User;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.InputStream;import java.util.Date;import java.util.List;//测试mybatis的crud方法public class MybatisTest { private InputStream in ; private SqlSessionFactory factory; private SqlSession session; private IUserDao userDao; // 测试查询所有方法 @Before //在测试之前执行 public void init()throws Exception { //1.读取配置文件 in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.创建 SqlSession 工厂对象 factory = builder.build(in); //4.创建 SqlSession 对象 session = factory.openSession(); //5.创建 Dao 的代理对象 userDao = session.getMapper(IUserDao.class); } //根据id来查询 @Test public void testFindOne() { //6.执行操作 User user = userDao.findbyid(41); System.out.println(user); } @Test public void saveuser(){ User user = new User(); user.setUsername("User property"); user.setAddress("北京市顺义区"); user.setSex("男"); user.setBirthday(new Date()); System.out.println("保存操作之前: "+user); //5.执行保存方法 userDao.saveuser(user); System.out.println("保存操作之后: "+user); } @Test public void updateuser(){ User user1 = userDao.findbyid(41); //更新操作 user1.setAddress("河南"); //更新用户 int i = userDao.updateUser(user1); System.out.println(user1); System.out.println(i); } @Test public void deleteuser(){ //根据id删除用户 // User user = userDao.findbyid(50); userDao.deleteUser(50); } @Test public void finduserbyname(){ List list = userDao.finguserbyname("王"); for (int i = 0; i < list.size(); i++) { User user = list.get(i); System.out.println(user); } } @Test public void findcount(){ int findtotal = userDao.findtotal(); System.out.println(findtotal); } //测试包装类作为参数,这个肯定远远不止这个简单的 @Test public void testfindbyQueryVo(){ //创建包装类 QueryVo vo = new QueryVo(); User user = new User(); //封装对象 user.setUsername("%王%"); vo.setUser(user); List list = userDao.findbyvo(vo); for (int i = 0; i < list.size(); i++) { User user1 = list.get(i); System.out.println(user1); } } @After//在测试方法执行完成之后执行 public void destroy() throws Exception{ //提交事务 session.commit(); //7.释放资源 session.close(); in.close(); }}

6.加一个包装类的情况QueryVo.java

package com.domain;import java.io.Serializable;public class QueryVo implements Serializable { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }}

总结:主要是IUserDao接口 IUserDao.xml和MybatisTest测试类之间的代码其他的变动不大

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:XWork- 命令模式框架
下一篇:简单实用golang框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~