mybatis入门程序

这篇博客介绍了MyBatis框架的入门步骤,包括导入必要的jar包,创建Customer持久化类,配置log4j,设计mapper.xml映射文件,编写mybatisconfig核心配置文件,以及编写测试类进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,导入相关jar包

2,创建持久化类 Costomer

3,创建log4j配置文件log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.itheima=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4,创建mybatis的mapper映射文件mapper.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.CustomerMapper">

 <select id="findCustomerById" parameterType="Integer" resultType="com.itheima.po.Customer">
        select * from t_customer where id = #{id}
    </select>

  <select id="findCustomerByName" parameterType="String" resultType="com.itheima.po.Customer">
        select * from t_customer where username like '%${value}%'
    </select>
   
    <insert id="addCustomer" parameterType="com.itheima.po.Customer">
        insert into t_customer(username,jobs,phone)
        value(#{username},#{jobs},#{phone})
    </insert>
 
    <update id="updateCustomer" parameterType="com.itheima.po.Customer">
        update t_customer set username=#{username},jobs=#{jobs},phone=#{phone} where id =#{id}
    </update>
    
    <delete id="deleteCustomer" parameterType="Integer">
        delete from t_customer where id=#{id}
    </delete>
  </mapper>


5,创建mybatisconfig核心配置文件mybatisconfig.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="mysql">
 <environment id="mysql">
 <transactionManager type="JDBC"/>
 <dataSource type="POOLED">
 <property name="driver" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
 <property name="username" value="root"/>
 <property name="password" value="root"/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 <mapper resource="com/itheima/mapper/CustomerMapper.xml"/>
 </mappers>

</configuration>


6,创建test测试类

package com.itheima.test;

import java.io.IOException;

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

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 com.itheima.po.Customer;

public class MybatisTest {
//    @Test
//    public void findCustomerByIdTest() throws IOException{
//        String resource = "mybatis-config.xml";
//        
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        
//        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        
//        SqlSession session = sessionFactory.openSession();
//        
//        Customer customer = session.selectOne("com.itheima.mapper"+".CustomerMapper.findCustomerById",1);
//        
//        System.out.println(customer.toString());
//        
//        session.close();
//        
//    }
    
//    @Test
//    public void findCustomerByUsernameTest() throws IOException{
//        String resource = "mybatis-config.xml";
//        
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        
//        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        
//        SqlSession session = sessionFactory.openSession();
//        
//        List<Customer> customers = session.selectList("com.itheima.mapper"+".CustomerMapper.findCustomerByName","j");
//        
//        for(Customer c:customers){
//            
//            System.out.println(c);
//        }
//        
//        session.close();
//        
//    }
    @Test
    public void adCustomerTest() throws IOException{
        String resource = "mybatis-config.xml";
        
        InputStream inputStream = Resources.getResourceAsStream(resource);
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        SqlSession session = sessionFactory.openSession();
        
        Customer customer =new Customer();
        //customer.setId(5);如果数据表的id不是自增,需要设置id,CustomerMapper.xml中的insert语句也需要设置
        customer.setUsername("晨");
        customer.setJobs("ow");
        customer.setPhone("45454454");
        
        int rows = session.insert("com.itheima.mapper"+".CustomerMapper.addCustomer", customer);
        if(rows>0){
            System.out.println("你成功插入"+rows+"条语句!");
        }
        else{
            System.out.println("插入失败");
        }
        session.commit();
        
        session.close();
        
    }
@Test
//    public void updateCustomerTest() throws IOException{
//        String resource = "mybatis-config.xml";
//        
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//        
//        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        
//        SqlSession session = sessionFactory.openSession();
//        
//        Customer customer =new Customer();
//        customer.setId(11);
//        customer.setUsername("wang");
//        customer.setJobs("teacher");
//        customer.setPhone("872912971");
//        session.update("com.itheima.mapper"+".CustomerMapper.updateCustomer", customer);
//        session.commit();
//        
//        session.close();
//        
//    }
    
    @Test
    public void deleteCustomerTest() throws IOException{
        String resource = "mybatis-config.xml";
        
        InputStream inputStream = Resources.getResourceAsStream(resource);
        
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
        SqlSession session = sessionFactory.openSession();
        
        
        session.delete("com.itheima.mapper"+".CustomerMapper.deleteCustomer", 11);
        session.commit();
        
        session.close();
        
    }
 }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值