文章目录
回顾接口+mapper.xml操作数据库Mapper编写有几种方式 ?案例演示利用注解传递参数MyBatis常用注解
模糊查询#{}和${}的区别
动态SQL简介动态SQL案例演示if的用法choose的用法foreach的用法trim, where, set的用法
sql块分页查询特殊字符处理mapper接口+注解的方式操作数据库支持驼峰命名
回顾
MyBatis(一)——MyBatis简介、MyBatis入门程序 MyBatis(二)——全局配置文件详解
接口+mapper.xml操作数据库
Mapper编写有几种方式 ?
接口实现类集成SQLSessionDaoSupport 此方法需要编写mapper接口,mapper接口的实现类,mapper.xml文件。使用org.mybatis.spring.mapper.MapperFactoryBean 此方法需要在SqlMapConfig.xml中配置mapper.xml的位置,还需定义mapper接口。使用mapper扫描器 需要编写mapper.xml文件,需要mapper接口,配置mapper扫描器,使用扫描器从spring容器中获取mapper的实现对象。
案例演示
总结: 使用Mybatis的mapper接口调用时候有哪些要求?
Mapper接口方法名和Mapper.xml中定义的每个SQL的id相同;Mapper接口方法的输入参数类型和mapper.xml中定义的每个sqlparameterType类型相同Mapper接口方法的输出参数类型和mapper.xml中定义的sql的resultType的类型相同Mapper.xml文件中的namespace,就是接口的类路径。
有两个问题值得注意,第一是接口名必须和映射文件名一致,第二是接口文件和mapper.xml的位置必须放置在同一目录下,只有这样MyBatis才会自动扫描,把接口和映射文件相匹配。否则,就要在全局配置文件中对接口和映射文件分别进行配置。这里我们推荐自动扫描,即把mapper.xml和接口文件放在同一目录下,配置时只需要扫描接口文件就可以了。 话不多说,继MyBatis(一)中的小程序,我们现在来新建一ProductMapper.java接口,此时的目录信息如下: 接着去全局配置文件中进行配置:
我们测试时按照从 映射文件——接口——测试类——控制台 的顺序依次编写代码:
映射文件:新增商品信息
insert into t_product values(
#{id},
#{name},
#{code},
#{price},
#{count},
#{description},
#{status},
#{create_time}
);
接口
package com.xx.mapper;
import com.xx.entity.Product;
public interface ProductMapper {
// 接口中定义方法,方法名和映射文件中的sql语句id尽量保持一致,该有的参数也要定义出来
public void insertProductInfo(Product product);
}
测试类
package com.xx.test;
import java.io.IOException;
import java.util.Date;
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.xx.entity.Product;
import com.xx.mapper.ProductMapper;
public class MyTest2 {
@Test
public void testInsertProductInfo() throws IOException {
// 1.获取到SqlSessionFactory对象
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsReader("mybatis-config.xml"));
// 2.获取SqlSession对象
SqlSession session = sessionFactory.openSession();
// 3.通过接口的class文件获得mapper
ProductMapper mapper = session.getMapper(ProductMapper.class);
// 4.创建product对象
Product product = new Product();
product.setName("魅族手机");
product.setCode("10009");
product.setPrice(4000.00);
product.setCount(10);
product.setDescription("国产手机品牌");
product.setStatus(1);
product.setCreate_time(new Date());
// 5.mapper执行sql语句
mapper.insertProductInfo(product);
// 6.手动提交事务
session.commit();
// 7.释放资源
session.close();
}
}
控制台
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 922872566.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3701eaf6]
==> Preparing: insert into t_product values( ?, ?, ?, ?, ?, ?, ?, ? );
==> Parameters: null, 魅族手机(String), 10009(String), 4000.0(Double), 10(Integer), 国产手机品牌(String), 1(Integer), 2019-04-19 00:36:54.986(Timestamp)
update t_product set price=#{price} where id = #{id}
接口
public void updatePriceByID(@Param(value="price")Double price,
@Param(value="id")Integer id);
测试类
@Test
public void testUpdateProductById() throws IOException {
// 获取到SqlSessionFactory对象
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(Resources.getResourceAsReader("mybatis-config.xml"));
// 获取SqlSession对象
SqlSession session = sessionFactory.openSession();
// 通过接口的class文件获得mapper
ProductMapper mapper = session.getMapper(ProductMapper.class);
// 根据id来更改价格
mapper.updatePriceById(5000.00, 3);
// 手动提交事务
session.commit();
// 释放资源
session.close();
}
控制台
==> Preparing: update t_product set price=? where id=?
==> Parameters: 5000.0(Double), 3(Integer)
|