总结重点几步:
配置日志文件(log4j.properties)
配置mybatis-config.xml文件
配置映射文件(CustomerMapper.xml)
测试类要先加载mybatis配置文件为数据流,然后构建SQLSessionFactory工厂,然后由该工厂的openSession方法获得SQLSession对象,再通过该对象执行相应的方法并传入参数,最后关闭SQLSession对象。
首先在数据库mybatis里面建立表t_customer并插入如下数据
创建项目
将需要用到的jar包添加到目录中并加入到工作路径
在src下的包com.itheima.po里创建持久化类Customer
package com.itheima.po;
public class Customer {
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
}
在src下创建日志文件log4j.properties
log4j.rootLogger=ERROR, stdout
log4j.logger.com.itheima=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在src下创建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="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?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8"/>
<property name="username" value="root" />
<property name="password" value="1111"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/itheima/mapper/CustomerMapper.xml"/>
</mappers>
</configuration>
在src下的包com.itheima.mapper下创建CustomerMapper.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 concat('%',#{value},'%')
</select>
</mapper>
测试类,src下的包com.itheima.test的MybatisTest
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 sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Customer customer = sqlSession.selectOne("com.itheima.mapper.CustomerMapper"
+ ".findCustomerById", 3);
System.out.println(customer);
sqlSession.close();
}
// @Test
public void findCustomerByNameTest() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Customer> customers = sqlSession.selectList("com.itheima.mapper"
+ ".CustomerMapper.findCustomerByName", "j");
customers.forEach(c->System.out.println(c));
sqlSession.close();
}
}
精确查询的效果
模糊查询的效果: