Mybatis------代理开发

2023-04-10 15:17:53

什么是代理开发呢。

传统方式实现 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<User> selectedById(int id);

    public Integer saverUser(User user);

    public  Integer deleteUser(int id);


}

里面有查询,增加,和删除。 

第三个很简单啦,Mybatis配置文件。

<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.xuexi.domain"/>
        <!-- <typeAlias type="com.xuexi.domain.User" alias="user"/>    -->
    </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/user?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

第四个写mysql的地方

<?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.xuexi.Mapper.UserMapper">

    <!--id 给这次查询取个名字,查询结果赋值到User这个实体类 -->
    <select id="Querry" resultType="com.xuexi.domain.User">
        select * from users;
    </select>


    <select id="selectedById" parameterType="int" resultType="com.xuexi.domain.User">
        select * from users where id = #{id};
    </select>

    <insert id="saverUser" parameterType="com.xuexi.domain.User">
        insert into users (id,name) values(#{id},#{name});

    </insert>

    <delete id="deleteUser" parameterType="int">
        delete from users where id = #{id};
    </delete>

</mapper>

第五个不用说了吧,写测试的地方

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<User> users = sqlSession.selectList("test.Querry");
        //代理开发
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        //UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> 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();
    }

}
  • 作者:makabaka12138
  • 原文链接:https://blog.csdn.net/makabaka12138/article/details/126198677
    更新时间:2023-04-10 15:17:53