一、依赖dependency以及数据库配置
-
依赖
-
数据库配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描包-->
<context:component-scan base-package="com.kk.controller, com.kk.mapper, com.kk.service"/>
<!--配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/usedata?serverTimezone=GMT" />
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="maxActive" value="16"/>
<property name="minIdle" value="5"/>
</bean>
<!--创建Mybatis SqlSessionFactory,和数据库连接池连接-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--加载mybatis的配置-->
<property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
</bean>
<!--Mybatis整合Spring,告诉spring,数据库代码存在位置(创建各自接口的动态代理类)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kk.mapper"/>
</bean>
<!--将数据源关联到事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务-->
<tx:annotation-driven/>
</beans>
二、知识点
4. resultType要求数据库中列名和pojo对象的属性名一模一样,面对不一样的情况可以用resultMap处理
示例:
1.1 在接口OrderMapper中
import models.Order;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface OrderMapper {
// 查询所有的订单
List<Order> queryAllOrderByResultMap();
}
1.2 在OrderMapper.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.kk.mapper.OrderMapper">
<resultMap id="baseOrderMap" type="com.kk.models.Order">
<id column="id" jdbcType="VARCHAR" property="idBean"/>
<result column="pid" jdbcType="INTEGER" property="pidBean"/>
<result column="userid" jdbcType="INTEGER" property="useridBean"/>
</resultMap>
<select id="queryAllOrderByResultMap" resultMap="baseOrderMap">
select * from `order`
</select>
</mapper>
1.3 在serviceImpl中
package com.kk.service;
import com.kk.mapper.OrderMapper;
import com.kk.models.Order;
import com.kk.models.Product;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Resource
private OrderMapper orderMapper;
@Override
public int generateOrder(Product product) {
Order order = new Order();
return 0;
}
@Override
public void queryAllOrderByResultMap() {
List<Order> orders = orderMapper.queryAllOrderByResultMap();
System.out.println(orders);
}
}
1.4 在测试类中
package com.test;
import com.kk.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@ContextConfiguration(locations = {"classpath:spring/spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderControllerTest {
@Resource
private OrderService orderService;
@Test
public void testQueryAllOrder() {
orderService.queryAllOrderByResultMap();
}
}
1.5 查询结果