Dao layer implementation of Mybatis-agent development method

Insert picture description here

Insert picture description here
Insert picture description here
Your UserMapper.xml has multiple sql (such as adding, deleting and modifying) statements, but your corresponding interface is incomplete (only the xml adding method is written) and there will be no errors when these sql methods are written.

Important: The parameter name of the interface is written casually regardless of whether the parameterType is an object or basically. XML requires the object to write the attribute name, basically just write it casually (the interface regardless of the parameter name, only the type of the parameter should be the same as the parameterType of xml):

The following mapper is the dao before, but the name is different
Insert picture description here
Sys_user:

package com.itheima.domain;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/28 12:18
 */
public class Sys_user {
    
    
    private int id;
    private String username;
    private String email;
    private String password;
    private String phoneNum;

    @Override
    public String toString() {
    
    
        return "Sys_user{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                ", phoneNum='" + phoneNum + '\'' +
                '}';
    }

    public int getId() {
    
    
        return id;
    }

    public void setId(int id) {
    
    
        this.id = id;
    }

    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    public String getEmail() {
    
    
        return email;
    }

    public void setEmail(String email) {
    
    
        this.email = email;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }

    public String getPhoneNum() {
    
    
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
    
    
        this.phoneNum = phoneNum;
    }
}

UserMapper:

package com.itheima.mapper;

import com.itheima.domain.Sys_user;

import java.util.List;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/30 11:06
 */
public interface UserMapper {
    
    
    public List<Sys_user> findAll();
    public Sys_user findone(int id2);  //由于parameterType是基本类型,那么参数名在接口和.xml都是随便写

    public int save(Sys_user user1);
}

ServiceDemo :

package com.itheima.service;

import com.itheima.domain.Sys_user;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import org.apache.ibatis.io.Resources;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/30 11:07
 */
public class ServiceDemo {
    
    

    @Test
    //查全部
    public void test1() throws IOException {
    
    
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<Sys_user> sys_userList = mapper.findAll();
        System.out.println(sys_userList);
    }


    @Test
    //测试查一条
    public void test2() throws IOException {
    
    
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Sys_user sys_use = mapper.findone(7);
        System.out.println(sys_use);
    }

    @Test
    //增
    public void test3() throws IOException {
    
    
        Sys_user sys_user=new Sys_user();
        sys_user.setUsername("喜洋洋");
        sys_user.setPhoneNum("555522");
        InputStream resourceAsStream= Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession=sqlSessionFactory.openSession(true);

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int count = mapper.save(sys_user);
        System.out.println(count);
    }
}

The test is successful, demonstrate test3:
Insert picture description hereInsert picture description here

UserMapper.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.itheima.mapper.UserMapper">
    <!--查询数据表封装成集合输出-->
    <select id="findAll" resultType="com.itheima.domain.Sys_user">   /*Sys_user是数据库对应的实体类*/
        SELECT * FROM sys_user  /*这里是sys_user是数据库真实的表*/
    </select>

    <!--查询一个对象数据-->
    <select id="findone" resultType="com.itheima.domain.Sys_user" parameterType="int">   /*Sys_user是数据库对应的实体类*/
        SELECT * FROM sys_user WHERE id=#{id1}/*这里是sys_user是数据库真实的表*/
    </select>

    <!--插入操作,save是随便起的,parameterType后面都是写对象类型进行传递的哦,这里的user是对应sqlMapConfig的别名-->
    <insert id="save" parameterType="user">
        /*这里#后的{}写的是对应上面parameterType对象里面的属性,而不是对应数据库表的哦
        (虽然一般数据库表跟实体类设置成一样,但有时不一样,这里要写的是实体类的哦)*/
        INSERT INTO sys_user VALUES(#{id},#{username},#{email},#{password},#{phoneNum})
    </insert>



</mapper>

jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

sqlMapConfig.xml:

<?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标签加载外部properties文件-->
    <properties resource="jdbc.properties"></properties>

    <!--自定义别名-->
    <typeAliases>
        <typeAlias type="com.itheima.domain.Sys_user" alias="user"></typeAlias>
        <!--这里还可以配多个-->
    </typeAliases>

    <!--数据源环境-->
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>

            </dataSource>
        </environment>
    </environments>

    <!--加载映射文件-->
    <mappers>
        <mapper resource="com.itheima.mapper/UserMapper.xml"></mapper>
    </mappers>
</configuration>

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.itcast</groupId>
    <artifactId>Mybatis_day2_dao</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    
</project>

Guess you like

Origin blog.csdn.net/GLOAL_COOK/article/details/113414561