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();
}
}