文章图片
【java|Mybatis------代理开发】什么是代理开发呢。
传统方式实现 DAO 层,需要写接口和实现类。采用 Mybatis 的代理开发方式实现 DAO 层的开发,只需要编写 Mapper 接口(相当于 Dao 接口),由 Mybatis 框架根据接口定义创建接口的动态代理对象
文章图片
第一个是对接数据库的,比如我的数据库有一个表是id + name。
package com.xuexi.domain;
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String name;
public void setId(int id) {
this.id = id;
}public void setName(String name) {
this.name = name;
}public int getId() {
return id;
}public String getName() {
return name;
}@Override
public String toString() {
return "id: "+ id+" name:" + name;
}
}
文章图片
第二个就是mapper接口了。只需要编写 Mapper 接口(相当于 Dao 接口)
package com.xuexi.Mapper;
import com.xuexi.domain.User;
import java.util.List;
public interface UserMapper {
List selectedById(int id);
public Integer saverUser(User user);
publicInteger deleteUser(int id);
}
文章图片
里面有查询,增加,和删除。
第三个很简单啦,Mybatis配置文件。
文章图片
第四个写mysql的地方
select * from users;
select * from users where id = #{id};
insert into users (id,name) values(#{id},#{name});
delete from users where id = #{id};
文章图片
第五个不用说了吧,写测试的地方
import com.xuexi.Mapper.UserMapper;
import com.xuexi.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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Mytest{static SqlSession sqlSession;
static {String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象,用它来执行sql
sqlSession = sqlSessionFactory.openSession();
}@Test
public void test() throws Exception
{
//获取路径
//3. 执行sql
//List users = sqlSession.selectList("test.Querry");
//代理开发
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List users = usermapper.selectedById(1);
System.out.println(users);
//4. 释放资源
sqlSession.close();
}@Test
public void inserttest() throws Exception
{
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user1 = new User();
user1.setId(7);
user1.setName("小青龙");
Integer success =usermapper.saverUser(user1);
sqlSession.commit();
if(success <=0)
{
System.out.println("失败了!");
}
else
{
System.out.println("成功了!");
}
//4. 释放资源
sqlSession.close();
}@Test
public void deletetest() throws Exception
{
UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
//UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Integer success =usermapper.deleteUser(7);
sqlSession.commit();
if(success <=0)
{
System.out.println("失败了!");
}
else
{
System.out.println("成功了!");
}
//4. 释放资源
sqlSession.close();
}}
推荐阅读
- intellij-idea|编写MyBatis-Plus代码
- 数据库|mybatis-plus QueryWrapper 添加limit
- 【Java面试】这道互联网高频面试题难住了80%的程序员(索引什么时候失效?)
- Redis|缓存穿透、缓存击穿、缓存雪崩区别
- java|秒杀项目收获
- Redis|从底层看 Redis 的五种数据类型
- java|创作365天纪念日
- java|【纯享】剑指大厂,22年最强阿里JAVA手册
- java|Nginx 限流的天坑