mybatis xml配置使用方式,resultMap使用

项目结构

mybatis xml配置使用方式,resultMap使用_第1张图片

数据库表

mybatis xml配置使用方式,resultMap使用_第2张图片

引入依赖


            mysql
            mysql-connector-java
            8.0.30
        

        
            org.mybatis
            mybatis
            3.5.5
        

        
            junit
            junit
            4.12
            test
        
        
            junit
            junit
            4.12
            test
        

mybatis-config.xml 配置日志 数据源 映射





    
        
        
    

    
        
            
            
                
                
                
                
            
        
    

    
        
    

编写实体类

package com.tmg.domain;

public class Student {
    private int id;
    private String name;
    private int age;//提供构造器
    private String email;//提供set(),get(),toString(),hascode()等方法

    public Student(int id, String name, int age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public Student() {
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
}


控制数据库数据方法

package com.tmg.dao;

import com.tmg.domain.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentDao {

    void insert(Student employee);

    void update(Student employee);

    void deleteById(Long id);

    List selectAll();

    Student selectById(Long id);

    //多个参数的配置
    void insertEmp( @Param("stuName")  String name,
                   @Param("stuAge") int age, @Param("stuEmail")  String email);
}

StudentDao.xml







    
    
    
    
        insert into student( stu_id,stu_name,stu_age,stu_email) values (#{id},#{name},#{age},#{email});
    

    
        update student set stu_age=#{age},stu_email=#{email},stu_name=#{name} where stu_id=#{id} ;
    

    
        delete from student where stu_id=#{id};
    


    

    
        
        
        
        
        
        
    

    

    
        insert into student(stu_name,stu_age,stu_email) values (#{stuName},#{stuAge},#{stuEmail});
    

StudentDaoText 测试类

package com.tmg.dao;

import com.tmg.domain.Student;
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.util.List;

public class StudentDaoText {

    @Test
    public void testinsert() throws IOException {
        //创建会话工厂构建器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        //创建会话
        SqlSession sqlSession = factory.openSession();
        //获得Mapper对象
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);//通过jdk动态代理创建实现类,返回代理对象
        Student student = new Student(0, "zzz", 20, "[email protected]");
        mapper.insert(student);
        sqlSession.commit();
        System.out.println("insert--->"+student);


    }
    @Test
    public void testupdate() throws IOException {
        //创建会话工厂构建器
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        //创建会话
        SqlSession sqlSession = build.openSession();
        //获得Mapper对象
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = new Student(12, "zl", 99, "[email protected]");
        mapper.update(student);
        sqlSession.commit();
    }

    @Test
    public void testdeleteById() throws IOException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        mapper.deleteById(11L);
        sqlSession.commit();
    }

    @Test
    public void testselectAll() throws IOException {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = build.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List students = mapper.selectAll();
        for(Student student : students){
            System.out.println(student);
        }

    }
    @Test
    public void testselectById() throws IOException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = factory.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        Student student = mapper.selectById(3L);
        System.out.println("student---->"+student);

    }

    @Test
    public void testinsertEmp() throws IOException {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = factoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        SqlSession sqlSession = factory.openSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        mapper.insertEmp("tmg",18,"[email protected]");
        sqlSession.commit();
    }

}

你可能感兴趣的:(java,xml配置,mybatis,mybatis,xml,数据库)