MyBatis的前身的iBatis,是一个支持普通SQL查询、存储过程以及高级映射的持久层框架。其性能优异,具有高度的灵活性、可优化性和易于维护等特点。
MyBatis框架也被称之为ORM(对象关系映射)框架。ORM为一种为了解决面向对象与关系型数据库中数据类型不匹配的技术,通过描述java对象与数据库表之间的映射关系,自动将java应用程序中的对象持久化到关系型数据库的表中。
步骤:
导包(导入MyBatis的核心包和依赖包以及mysql的驱动包)
创建数据库表t_customer
创建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
在po包下创建一个Customer.java,属性和数据库表字段相对应。
package 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 + "]";
}
}
在mapper包下创建映射文件CustomerMapper.xml
创建MyBatis的核心配置文件mybatis-config.xml
在test包下创建MyBatisTest.java
根据用户编号查询部分
@Test
public void findCustomerByIdTest() throws Exception {
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream =
Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行映射文件中定义的SQL,并返回映射结果
Customer customer = sqlSession.selectOne("mapper"
+ ".CustomerMapper.findCustomerById", 1);
// 打印输出结果
System.out.println(customer.toString());
// 5、关闭SqlSession
sqlSession.close();
}
@Test
public void findCustomerByNameTest() throws Exception{
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行映射文件中定义的SQL,并返回映射结果
List customers = sqlSession.selectList("mapper"
+ ".CustomerMapper.findCustomerByName", "j");
for (Customer customer : customers) {
//打印输出结果集
System.out.println(customer);
}
// 5、关闭SqlSession
sqlSession.close();
}
其中根据客户编号精确查询客户信息的返回类型是Customer,调用方法是selectOne,返回的是一个用户
根据用户名模糊查询的返回类型的泛型,调用的方法是selectList
customerMapper.xml中添加如下映射
insert into t_customer(username,jobs,phone)
values(#{username},#{jobs},#{phone})
MyTest.java中添加测试代码
@Test
public void addCustomerTest() throws Exception{
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行添加操作
// 4.1创建Customer对象,并向对象中添加数据
Customer customer = new Customer();
customer.setUsername("rose");
customer.setJobs("student");
customer.setPhone("13333533092");
// 4.2执行SqlSession的插入方法,返回的是SQL语句影响的行数
int rows = sqlSession.insert("mapper"
+ ".CustomerMapper.addCustomer", customer);
// 4.3通过返回结果判断插入操作是否执行成功
if(rows > 0){
System.out.println("您成功插入了"+rows+"条数据!");
}else{
System.out.println("执行插入操作失败!!!");
}
// 4.4提交事务,若无提交事务,则无法将信息写入数据库表中
sqlSession.commit();
// 5、关闭SqlSession
sqlSession.close();
}
mapper文件修改如下
select LAST_INSERT_ID()
insert into t_customer(username,jobs,phone)
values(#{username},#{jobs},#{phone})
测试代码修改如下
@Test
public void addCustomerTest() throws Exception{
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行添加操作
// 4.1创建Customer对象,并向对象中添加数据
Customer customer = new Customer();
customer.setUsername("anny");
customer.setJobs("student");
customer.setPhone("13333533092");
// 4.2执行SqlSession的插入方法,返回的是SQL语句影响的行数
int rows = sqlSession.insert("mapper"
+ ".CustomerMapper.addCustomer", customer);
// 4.3通过返回结果判断插入操作是否执行成功
if(rows > 0){
System.out.println("您成功插入了"+rows+"条数据!");
}else{
System.out.println("执行插入操作失败!!!");
}
// 4.4提交事务,若无提交事务,则无法将信息写入数据库表中
sqlSession.commit();
System.out.println(customer.getId());
// 5、关闭SqlSession
sqlSession.close();
}
update t_customer set
username=#{username},jobs=#{jobs},phone=#{phone}
where id=#{id}
测试
@Test
public void updateCustomerTest() throws Exception{
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行更新操作
// 4.1创建Customer对象,对对象中的数据进行模拟更新
Customer customer = new Customer();
customer.setId(4);
customer.setUsername("rose");
customer.setJobs("programmer");
customer.setPhone("13311111111");
// 4.2执行SqlSession的更新方法,返回的是SQL语句影响的行数
int rows = sqlSession.update("mapper"
+ ".CustomerMapper.updateCustomer", customer);
// 4.3通过返回结果判断更新操作是否执行成功
if(rows > 0){
System.out.println("您成功修改了"+rows+"条数据!");
}else{
System.out.println("执行修改操作失败!!!");
}
// 4.4提交事务
sqlSession.commit();
// 5、关闭SqlSession
sqlSession.close();
}
!-- 删除客户信息 -->
delete from t_customer where id=#{id}
@Test
public void deleteCustomerTest() throws Exception{
// 1、读取配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 2、根据配置文件构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
// 3、通过SqlSessionFactory创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 4、SqlSession执行删除操作
// 4.1执行SqlSession的删除方法,返回的是SQL语句影响的行数
int rows = sqlSession.delete("mapper"
+ ".CustomerMapper.deleteCustomer", 4);
// 4.2通过返回结果判断删除操作是否执行成功
if(rows > 0){
System.out.println("您成功删除了"+rows+"条数据!");
}else{
System.out.println("执行删除操作失败!!!");
}
// 4.3提交事务
sqlSession.commit();
// 5、关闭SqlSession
sqlSession.close();
}