【MyBatis学习及总结02】MyBatis实现CRUD操作

文章目录

        • 1.创建环境
        • 2.写配置文件
        • 3.编写测试类

1.创建环境

1.导包:
【MyBatis学习及总结02】MyBatis实现CRUD操作_第1张图片
在resource目录下导入一个日志文件:



 
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 
 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
   <param name="Encoding" value="UTF-8" />
   <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
   layout>
 appender>
 <logger name="java.sql">
   <level value="debug" />
 logger>
 <logger name="org.apache.ibatis">
   <level value="info" />
 logger>
 <root>
   <level value="debug" />
   <appender-ref ref="STDOUT" />
 root>
log4j:configuration>

1.数据库和表
【MyBatis学习及总结02】MyBatis实现CRUD操作_第2张图片
2.封装数据库的JavaBean对象

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
    private Integer id;
    private String empName;
    private String email;
    private String gender;
}

3.操作数据库的dao接口,这个接口中增删改查方法

public interface EmployeeDao {
    public int insertEmployee(Employee employee);
    public boolean deleteEmployee(int id);
    public int updateEmployee(Employee employee);
    public Employee getEmpById(int id);
}

2.写配置文件

1.写全局配置文件:mybatis-config.xml


        
<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_01?
                useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
            <property name="username" value="root"/>
            <property name="password" value="123"/>
        dataSource>
    environment>
environments>
configuration>

2.写映射配置文件:EmployeeDao.xml



<mapper namespace="com.hh.dao.EmployeeDao">
    
    <select id="getEmpById" resultType="com.hh.bean.Employee">
        select * from t_employee where  id=#{id}
    select>

    <update id="updateEmployee">
        update t_employee
        set empname=#{empName},gender=#{gender},email=#{email} where id=#{id}
    update>

    <delete id="deleteEmployee">
        delete from t_employee where id=#{id}
    delete>
    
    <insert id="insertEmployee">
        insert into t_employee(empname,gender,email) values (#{empName},#{gender},#{email})
    insert>
mapper>

3.将映射配置文件在全局配置文件中注册:


<mappers>
    <mapper resource="com/hh/dao/EmployeeDao.xml"/>
mappers>

3.编写测试类

对于查询数据库中数据

public class MyTest {
    SqlSessionFactory sqlsessionFactory;
    @Before
    public void initSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream =
                Resources.getResourceAsStream(resource);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        sqlsessionFactory  = builder.build(inputStream);
    }

    @Test
    public void testSelect() throws IOException {
        SqlSession sqlSession = sqlsessionFactory.openSession();
        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
        Employee employee = mapper.getEmpById(1);
        System.out.println(employee);
    }
}

在这里插入图片描述
对于添加数据,删除数据,修改数据都需要手动提交commit
首先添加数据:

@Test
    public void testInsert(){
        SqlSession sqlSession = sqlsessionFactory.openSession();
        try {
            EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
            int i = mapper.insertEmployee(
                    new Employee(null, "tomcat", "[email protected]", "0"));
            System.out.println(i);
        }finally {
            //手动提交
            sqlSession.commit();
            sqlSession.close();
        }
    }

【MyBatis学习及总结02】MyBatis实现CRUD操作_第3张图片
修改数据:

@Test
public void testUpdate(){
    SqlSession sqlSession = sqlsessionFactory.openSession();
    try {
        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
        mapper.updateEmployee(new Employee(6,"gutianle","[email protected]","1"));
    } finally {
        //手动提交
        sqlSession.commit();
        sqlSession.close();
    }
}

【MyBatis学习及总结02】MyBatis实现CRUD操作_第4张图片
删除数据:

@Test
public void testDelete(){
    SqlSession sqlSession = sqlsessionFactory.openSession();
    try {
        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
        mapper.deleteEmployee(6);
    } finally {
        //手动提交
        sqlSession.commit();
        sqlSession.close();
    }
}

【MyBatis学习及总结02】MyBatis实现CRUD操作_第5张图片

你可能感兴趣的:(MyBatis)