Mybatis Mapper代理开发Dao层

简介: Mybatis Mapper代理开发Dao层

Mapper代理开发原因


  1. 程序员只需要编写mapper接口和mapper.xml映射文件,Mybatis可以自动生成mapper接口实现类代理对象。
  2. 只不过程序员在编写mapper接口时 需要遵循一些开发规范:


1.mapper.java接口名跟mapper.xml映射文件相同并且在同一个包下
2 . 在mapper.xml映射文件中,namespace="mapper接口地址" --完全包名.mapper接口名
3 . mapper.java接口中的方法名和mapper.xml中statement的id值一致
3. mapper.java接口中的方法输入参数类型和mapper.xml中statement和parameterType指定的类型一致
4. mapper.java接口中的方法返回值类型和mapper.xml中statement的resultType指定的类型一致
5. SqlMapconfig.xml配置文件中--由于使用mapper代理方式,改为加载mapper接口。

传统方式使用mabatis进行Dao开发:

编写配置文件->编写mabatis映射文件->dao接口->dao实现类(里面靠sqlfactory实现各种接口)

Mapper代理模式进行Dao层开发

SqlMapperConfig核心配置文件:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties"/>
    <typeAliases>
        <package name="com.msb.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="cl091900"/>
            </dataSource>
        </environment>
    </environments>
   <mappers>
       <mapper class="com.msb.mapper.DeptMapper"></mapper>
   </mappers>
</configuration>

DeptMapper.xml

<?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.msb.mapper.DeptMapper">
<!--    id要和方法名相同-->
    <select id="selectAll" resultType="dept">
        select * from dept
    </select>
    <select id="selectOneById" resultType="dept"  parameterType="int">
        select * from dept where deptno=#{id}
    </select>
    <insert id="insertDept" parameterType="dept" >
        insert  into dept(deptno,dname,loc) values (#{deptno},#{dname},#{loc})
    </insert>
    <update id="updateDept" parameterType="dept">
        update dept set dname =#{dname},loc =#{loc} where deptno=#{deptno}
    </update>
    <delete id="deleteDept"   parameterType="int">
        delete from dept where deptno= #{id}
    </delete>
 </mapper>

DeptMapper接口

public interface DeptMapper {
    //查询部门信息
    List<Dept> selectAll() throws Exception;
    //根据主键查询单个部门信息
    Dept selectOneById(int id) throws Exception;
    int insertDept(Dept dept) throws Exception;
    int updateDept(Dept dept) throws Exception;
    int deleteDept(int id) throws Exception;
}
  • 测试类:
public class Test {
    private SqlSessionFactory factory;
    @Before
    public void init() throws IOException{
        InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
    }
    @org.junit.Test
    public void test() throws Exception {
        DeptMapper deptMapper=factory.openSession().getMapper(DeptMapper.class);
        System.out.println(deptMapper.selectOneById(1));
    }
}
相关文章
|
15天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
1月前
|
SQL Java 数据库连接
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
文章讲述了在使用Mybatis时遇到的资源文件找不到的问题,并提供了通过修改Maven配置来解决资源文件编译到target目录下的方法。
Mybatis系列之 Error parsing SQL Mapper Configuration. Could not find resource com/zyz/mybatis/mapper/
|
27天前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
30 1
|
30天前
|
SQL Java 数据库连接
Spring Boot联手MyBatis,打造开发利器:从入门到精通,实战教程带你飞越编程高峰!
【8月更文挑战第29天】Spring Boot与MyBatis分别是Java快速开发和持久层框架的优秀代表。本文通过整合Spring Boot与MyBatis,展示了如何在项目中添加相关依赖、配置数据源及MyBatis,并通过实战示例介绍了实体类、Mapper接口及Controller的创建过程。通过本文,你将学会如何利用这两款工具提高开发效率,实现数据的增删查改等复杂操作,为实际项目开发提供有力支持。
59 0
|
1月前
|
SQL Java 数据库连接
后端框架的学习----mybatis框架(7、使用注解开发)
这篇文章讲述了如何使用MyBatis框架的注解方式进行开发,包括在接口上使用注解定义SQL语句,并通过动态代理实现对数据库的增删改查操作,同时强调了接口需要在核心配置文件中注册绑定。
|
1月前
|
XML Java 数据库连接
Mybatis 模块拆份带来的 Mapper 扫描问题
Mybatis 模块拆份带来的 Mapper 扫描问题
31 0
|
2月前
|
SQL
自定义SQL,可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,如何自定义SQL呢?利用MyBatisPlus的Wrapper来构建Wh,在mapper方法参数中用Param注
自定义SQL,可以利用MyBatisPlus的Wrapper来构建复杂的Where条件,如何自定义SQL呢?利用MyBatisPlus的Wrapper来构建Wh,在mapper方法参数中用Param注
|
2月前
|
Java 数据库连接 mybatis
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
|
2月前
|
Java 数据库连接 Maven
Private method ‘getVideoList()‘ is never used,mybatis必须指定Mapper文件和实体目录,在参考其他人写的代码,要认真分析别人的代码,不要丢失
Private method ‘getVideoList()‘ is never used,mybatis必须指定Mapper文件和实体目录,在参考其他人写的代码,要认真分析别人的代码,不要丢失
|
14天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)