所谓不使用动态代理是指在 Dao 层需要我们自己来创建 Dao 层的接口与接口实现类。 在接口实现类的方法中我们自己通过调用 SqlSession 对象的方法完成数据库的操作
在 MyBatis 中提供了另外一种 Dao 层的实现方式,既:Mapper 动态代理(或称为接口 绑定)的操作方式。这种方式下程序员只需要写 Dao 接口,不需要创建 Dao 的接口实现类, Mybatis 会自动生成接口实现类的代理对象。在 Dao 层我们只要创建接口与映射配置文件即 可。这种方式可以大大简化 Dao 层的代码结构,是在开发中最常见的使用方式
@Getter @Setter public class Users { private int userid; private String username; private String usersex; }
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bjsxt jdbc.username=root jdbc.password=root
public class MybatisUtils { private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>(); private static SqlSessionFactory sqlSessionFactory = null; static{ //创建 SqlSessionFactory InputStream is = null; try{ is = Resources.getResourceAsStream("mybatis-cfg.xml"); }catch (IOException e){ e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } //获取 SqlSession public static SqlSession getSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession == null){ sqlSession = sqlSessionFactory.openSession(); threadLocal.set(sqlSession); } return sqlSession; } //关闭 SqlSession public static void closeSqlSession(){ SqlSession sqlSession = threadLocal.get(); if(sqlSession != null){ sqlSession.close(); threadLocal.set(null); } } }
public interface UsersMapper { List<Users> selectUsersAll(); }
<?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"> <mapper namespace="com.bjsxt.mapper.UsersMapper"> <!--查询所有用户--> <select id="selectUsersAll" resultType="users"> select * from users </select> </mapper>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--引入 db.properties 属性文件--> <properties resource="db.properties"/> <!--配置别名--> <typeAliases> <package name="com.bjsxt.pojo"/> </typeAliases> <!--配置环境--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!--引入 Mapper 映射配置文件--> <mappers> <package name="com.bjsxt.mapper"/> </mappers> </configuration>"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.bjsxt.mapper.UsersMapper"> </mapper>
public interface UsersService { List<Users> findUsersAll(); }
public class UsersServiceImpl implements UsersService { @Override public List<Users> findUsersAll() { List<Users> list = null; try{ SqlSession sqlSession = MybatisUtils.getSqlSession(); //getMapper 方法的作用是根据给定的接口的 Class 对象,生成该接口的接 口实现类的代理对象 UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); list = mapper.selectUsersAll(); }catch(Exception e){ e.printStackTrace(); }finally{ MybatisUtils.closeSqlSession(); } return list; } }
public class SelectUsersAllTest { public static void main(String[] args) { UsersService usersService = new UsersServiceImpl(); List<Users> list = usersService.findUsersAll(); list.forEach(System.out::println); } }