欠失は、検索データベース操作を変更 - MyBatisの持つフレームを達成するために



Employee.java

package com.atguigu.bean;

public class Employee {
	
	private Integer id;
	private String empName;
	private String email;
	private Integer gender;

	public Employee() {

	}
	public Employee(Integer id, String empName, String email, Integer gender) {
		this.id = id;
		this.empName = empName;
		this.email = email;
		this.gender = gender;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}

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

	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", empName=" + empName + ", email="
				+ email + ", gender=" + gender + "]";
	}
}

EmployeeDao.java

package com.atguigu.dao;

import com.atguigu.bean.Employee;

public interface EmployeeDao {

   public Employee getEmpById(Integer id);
   public int updateEmployee(Employee employee);
   public boolean deleteEmployee(Integer id);
   public int insertEmployee(Employee employee);
}

MyBatisの-config.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>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 配置连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis_0325"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

     <!-- mappers中注册我们所有写的dao接口的实现文件 -->
    <mappers>
        <!--resource:表示从类路径下找资源  -->
        <mapper resource="EmployeeDao.xml"/>
    </mappers>

</configuration>

EmployeeDao.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">

<!--namespace:名称空间;写接口的全类名,相当于告诉MyBatis这个配置文件是实现哪个接口的;  -->
<mapper namespace="com.atguigu.dao.EmployeeDao">
    <!--public Employee getEmpById(Integer id);  参数类型不用写 -->
    <!-- sql语句中不要写分号 -->
    <select id="getEmpById" resultType="com.atguigu.bean.Employee">
        select * from t_employee where id=#{id}
    </select>
    <!-- 增删改不用写返回值类型,增删改返回的是影响多少行
         mybatis会自动判断,如果是数字类型(int,long);如果是boolean(影响0行自动封装成false,否则为true)
    -->
    <!--
        #{属性名} 从传入的参数对象中取出对应属性的值
    -->
    <!--public int updateEmployee(Employee employee);-->
    <update id="updateEmployee">
        UPDATE t_employee
          set empname=#{empName},gender=#{gender},email=#{email}
          WHERE id=#{id}
    </update>
    <!--public boolean deleteEmployee(Integer id);-->
    <delete id="deleteEmployee">
        DELETE FROM t_employee WHERE id=#{id}
    </delete>
    <!--public int insertEmployee(Employee employee);-->
    <insert id="insertEmployee" >
        INSERT  INTO t_employee(empname,gender,email)  VALUES (#{empName},#{gender},#{email})
    </insert>
</mapper>

MyBatisCRUDTest.java

package com.atguigu.test;

import com.atguigu.bean.Employee;
import com.atguigu.dao.EmployeeDao;
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.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisCRUDTest {
    //工厂一个
    SqlSessionFactory sqlSessionFactory;
    @Before
    public void initSqlSessionFactory() throws IOException {
        //1、根据全局配置文件先得到SqlSessionFactory对象
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    /**
     * 测试更新
     */
    @Test
    public void testUpdate(){
        //1.获取和数据库的一次会话
        //自动提交
        SqlSession openSession = sqlSessionFactory.openSession(true);
        try {
            //2、获取到接口的映射器
            EmployeeDao mapper = openSession.getMapper(EmployeeDao.class);
            //3、测试
            int i = mapper.updateEmployee(new Employee(4, "止兮545", "[email protected]", 0));
            System.out.println(i);
        }finally {
            openSession.close();
        }
    }
    /**
     * 测试删除
     */
    @Test
    public void testDelete(){
        //1.获取和数据库的一次会话
        //自动提交
        SqlSession openSession = sqlSessionFactory.openSession(true);
        try {
            //2、获取到接口的映射器
            EmployeeDao mapper = openSession.getMapper(EmployeeDao.class);
            //3、测试
            boolean i = mapper.deleteEmployee(5);
            System.out.println(i);
        }finally {
            openSession.close();
        }
    }

    /**
     * 测试插入
     */
    @Test
    public void testInsert(){
        //1.获取和数据库的一次会话
          //自动提交
        SqlSession openSession = sqlSessionFactory.openSession(true);
        try {
            //2、获取到接口的映射器
            EmployeeDao mapper = openSession.getMapper(EmployeeDao.class);
            //3、测试
            int i = mapper.insertEmployee(new Employee(null, "止兮", "[email protected]", 0));
            System.out.println(i);
        }finally {
            //手动提交
            //openSession.commit();
            openSession.close();
        }
    }

    /**
     * 测试查询
     */
    @Test
    public void test() throws IOException {
        //1、根据全局配置文件先得到SqlSessionFactory对象
        //initSqlSessionFactory();  //有注解@Before在这个方法上
        //2、得到SqlSession对象,获取和数据库的一次会话;getConnection();
        SqlSession openSession = sqlSessionFactory.openSession();
        //3、使用SqlSession操作数据库,获取到dao接口的实现
        EmployeeDao employeeDao = openSession.getMapper(EmployeeDao.class);

        Employee employee;
        try {

            //4、调用之前的方法
            employee = employeeDao.getEmpById(1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }
}

公開された434元の記事 ウォンの賞賛105 ・は 70000 +を見て

おすすめ

転載: blog.csdn.net/qq_39368007/article/details/104958448