下面就是详细讲解“Spring如何集成ibatis项目并实现dao层基类封装”的完整攻略。
目录
Spring和iBatis的集成
Spring和iBatis的集成需要以下几个步骤:
- 引入Spring和iBatis相关的jar包
- 配置数据源
- 配置SqlMapClient和SqlSessionFactoryBean
- 配置事务管理器
- 配置Dao层的实现类
下面是一个基本的Spring和iBatis的集成配置:
<!-- 数据源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- iBatis配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sql-map-config.xml"/>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Dao层实现类配置 -->
<bean id="userDao" class="com.example.dao.impl.UserDaoImpl">
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate" />
</bean>
示例一:使用iBatis进行单表操作
假设我们要使用iBatis实现对用户表(user)的CRUD操作,需要以下三个文件:
- 定义sql语句的映射文件(user.xml)
<!-- user.xml -->
<sqlMap namespace="User">
<resultMap id="UserResultMap" class="UserDO">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="getUserById" resultMap="UserResultMap" parameterClass="java.lang.Integer">
select * from user where id = #id#
</select>
<insert id="insertUser" parameterClass="UserDO">
INSERT INTO user (id, name, age) VALUES (#id#, #name#, #age#);
</insert>
<update id="updateUser" parameterClass="UserDO">
UPDATE user SET name=#name#, age=#age# WHERE id=#id#
</update>
<delete id="deleteUser" parameterClass="java.lang.Integer">
DELETE FROM user WHERE id=#id#
</delete>
</sqlMap>
- 定义POJO对象(UserDO.java)
public class UserDO {
private Integer id;
private String name;
private Integer age;
// getters and setters ...
}
3.定义Dao层接口(UserDao.java)和实现类(UserDaoImpl.java)
public interface UserDao {
UserDO getUserById(Integer id);
void insertUser(UserDO user);
void updateUser(UserDO user);
void deleteUser(Integer id);
}
public class UserDaoImpl implements UserDao {
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
@Override
public UserDO getUserById(Integer id) {
return (UserDO)sqlMapClientTemplate.queryForObject("User.getUserById", id);
}
@Override
public void insertUser(UserDO user) {
sqlMapClientTemplate.insert("User.insertUser", user);
}
@Override
public void updateUser(UserDO user) {
sqlMapClientTemplate.update("User.updateUser", user);
}
@Override
public void deleteUser(Integer id) {
sqlMapClientTemplate.delete("User.deleteUser", id);
}
}
其中,getUserById方法用来根据id查询用户,insertUser方法用来插入用户,updateUser方法用来更新用户,deleteUser方法用来删除用户。
示例二:使用iBatis进行多表操作
假设我们要使用iBatis实现对订单(order)和明细表(order_item)的连表查询操作,需要以下文件:
- 定义sql语句的映射文件(order.xml)
<!-- order.xml -->
<sqlMap namespace="Order">
<resultMap id="OrderResultMap" class="OrderDO">
<result property="id" column="order_id" />
<result property="userId" column="user_id" />
<result property="orderNo" column="order_no" />
<result property="createTime" column="create_time" />
</resultMap>
<resultMap id="OrderItemResultMap" class="OrderItemDO">
<result property="id" column="id" />
<result property="orderId" column="order_id" />
<result property="goodsId" column="goods_id" />
<result property="price" column="price" />
<result property="num" column="num" />
</resultMap>
<select id="getOrderDetailById" resultMap="OrderResultMap" parameterClass="java.lang.Integer">
SELECT o.id AS order_id, o.user_id, o.order_no, o.create_time, oi.id, oi.goods_id, oi.price, oi.num
FROM `order` o
JOIN order_item oi ON oi.order_id = o.id
WHERE o.id = #id#
</select>
</sqlMap>
- 定义POJO对象(OrderDO.java和OrderItemDO.java)
public class OrderDO {
private Integer id;
private Integer userId;
private String orderNo;
private Date createTime;
private List<OrderItemDO> orderItemList;
// getters and setters ...
}
public class OrderItemDO {
private Integer id;
private Integer orderId;
private Integer goodsId;
private BigDecimal price;
private Integer num;
// getters and setters ...
}
- 定义Dao层接口(OrderDao.java)和实现类(OrderDaoImpl.java)
public interface OrderDao {
OrderDO getOrderDetailById(Integer id);
}
public class OrderDaoImpl implements OrderDao {
private SqlMapClientTemplate sqlMapClientTemplate;
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
@Override
public OrderDO getOrderDetailById(Integer id) {
OrderDO order = (OrderDO)sqlMapClientTemplate.queryForObject("Order.getOrderDetailById", id);
if (order != null) {
List<OrderItemDO> itemList = new ArrayList<>();
for (OrderItemDO item : order.getOrderItemList()) {
if (item.getOrderId().equals(order.getId())) {
itemList.add(item);
}
}
order.setOrderItemList(itemList);
}
return order;
}
}
Dao层基类封装
在以上两个示例中,我们可以发现Dao层的实现类都需要注入SqlMapClientTemplate并定义对应的增删改查方法,这部分内容可以进行基类的封装,这样每个Dao层实现类就不需要重复定义。
例如,我们可以定义一个BaseDao类,提供一些通用的SQL方法:
public class BaseDao<T> {
protected SqlMapClientTemplate sqlMapClientTemplate;
private final String NAMESPACE;
public BaseDao(SqlMapClientTemplate sqlMapClientTemplate, String namespace) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
this.NAMESPACE = namespace;
}
public int insert(String statementId, T entity) {
return sqlMapClientTemplate.insert(NAMESPACE + "." + statementId, entity);
}
public int update(String statementId, T entity) {
return sqlMapClientTemplate.update(NAMESPACE + "." + statementId, entity);
}
public int delete(String statementId, Object parameter) {
return sqlMapClientTemplate.delete(NAMESPACE + "." + statementId, parameter);
}
public List<T> queryForList(String statementId, Object parameter) {
return sqlMapClientTemplate.queryForList(NAMESPACE + "." + statementId, parameter);
}
public T queryForObject(String statementId, Object parameter) {
return (T)sqlMapClientTemplate.queryForObject(NAMESPACE + "." + statementId, parameter);
}
}
然后,在具体的Dao层实现类中继承BaseDao类,提供相关的操作。
例如,UserDaoImpl可以这么写:
public class UserDaoImpl extends BaseDao<UserDO> implements UserDao {
public UserDaoImpl(SqlMapClientTemplate sqlMapClientTemplate) {
super(sqlMapClientTemplate, "User");
}
@Override
public UserDO getUserById(Integer id) {
return queryForObject("getUserById", id);
}
@Override
public void insertUser(UserDO user) {
insert("insertUser", user);
}
@Override
public void updateUser(UserDO user) {
update("updateUser", user);
}
@Override
public void deleteUser(Integer id) {
delete("deleteUser", id);
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring如何集成ibatis项目并实现dao层基类封装 - Python技术站