插播一个:idea中配置junit4测试:
首先添加junit依赖,然后四步走。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
1.安装插件
–>打开File菜单的下拉菜单settings[设置]
–>点击左侧Plugins[插件]菜单
–>在输入框中输入JUnitGenerator 2.0进行Install
–>重启IDEA
2.配置插件
打开settings[设置]
–>点击左侧Other Settings[其他]菜单
–>点击左侧JUnit Generator菜单
–>修改Output Path[输出路径]为${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}
–>修改Default Template[默认模板]为JUnit4
–>点击JUnit4(页签 )
–>将package test.$entry.packageName;修改成package $entry.packageName;
–>点击OK[确定]保存并退出设置
3.点击选中相应的方法:右击,选择generate,在选中junit即可生成测试方法的模板
使用快捷键测试:双击选中要测试的方法或类,按 Ctrl + Shift + T ,选中create new test即可
4.运行后的代码路径和效果如图:
生成的junit测试方法会有红色提示,file was loades wrong。。。。。和包名有下划线。。。。。但这些并不影响junit测试的正常运行,实在觉得不爽,可自行优化 (将test设为普通文件,取消它的source Root Test,将java设为source Root Test即可):
修改后的截图如下:
5.测试代码:
public class OrdersMapperCustomImplTest {
private ApplicationContext applicationContext;
@Before
public void before() throws Exception {
applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@After
public void after() throws Exception {
}
/**
*
* Method: findOrdersPlusUser()
*
*/
@Test
public void testFindOrdersPlusUser() throws Exception {
//TODO: Test goes here...
OrdersMapperCustom ordersMapperCustom= (OrdersMapperCustom) applicationContext.getBean("ordersMapperCustom");
List<OrdersCustom> orderslist= ordersMapperCustom.findOrdersPlusUser();
System.out.println(orderslist);
System.out.println(orderslist);
}
}
整合思路:
1.spring通过单例方式管理sqlsessionFactory(need do)
2.spring和mybatis整合会自动生成代理对象,并使用sqlsessionFactory创建sqlsession
(spring和mybatis整合生成的代理对象会自动使用sqlsessionFactory生成sqlssion对象)
//这一步是spring干的事,我们管不着
3.spring管理持久层的mapper。(need do)
下面开始:
1.1
原始dao开发,需要在实现类中通过sqlsessionfactory创建sqlsession,
spring原始dao开发,需在实现类中通过spring注入sqlsessionfactory,这里通过声明式注入
目录结构:
OrdersMapperCustom接口类:
public interface OrdersMapperCustom {
public List<OrdersCustom>findOrdersPlusUser()throws Exception;
}
OrdersMapperCustom实现类:(通过继承SqlSessionDaoSupport,通过this.getSqlSession()语句,使得spring自动使用sqlsessionFactory完成生成sqlssion对象的过程)
public class OrdersMapperCustomImpl extends SqlSessionDaoSupport implements OrdersMapperCustom {
@Override
public List<OrdersCustom> findOrdersPlusUser() throws Exception {
SqlSession sqlSession=this.getSqlSession();
List<OrdersCustom> ordersCustom= sqlSession.selectList("findOrdersPlusUser",null);
return ordersCustom;
}
}
几大实体类,在一对多的文章里都有,可自行参考:(都省略getter,setter方法)
Orders类:
public class Orders {
private Integer id;
private String userId;
private String number;
private Date createtime;
private String note;
}
ordersCustom类:
public class OrdersCustom extends Orders {
private String name;
private String sex;
private String address;
private User user;
private List<Orderdetail> orderDetails;
}
ordersDetail类:
public class Orderdetail {
private Integer id;
private Integer ordersId;
private Integer itemsId;
private Integer itemsNum;
private Items items;
}
User类:
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
}
UserCustom类:
public class UserCustom extends User {
private List<Orders> ordersList;
public List<Orders> getOrdersList() {
return ordersList;
}
public void setOrdersList(List<Orders> ordersList) {
this.ordersList = ordersList;
}
}
Items类:
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
}
创建mybatis的全局配置文件,sqlMapConfig
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- <typeAlias type="cn.itcast.ssm.po.User" alias="User"/>-->
<package name="cn.itcast.ssm.po"/>
<!-- 分页 -->
<!-- <typeAlias type="com.fh.entity.Page" alias="Page"/>-->
</typeAliases>
<mappers>
<mapper resource="sqlmap/OrdersMapperCustom.xml"/>
</mappers>
</configuration>
spring的applicationContext.xml:(在里面配置声明dao层的接口实现类bean,它依赖于sqlSessionFactory,这就是spring的接管)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<!-- 自动扫描 将Mapper接口生成代理注入到Spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wego.mapper" />
</bean>
<!-- 声明式,spring原始dao开发-->
<bean id="ordersMapperCustom" class="cn.itcast.ssm.dao.OrdersMapperCustomImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
ordermapperCustom.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.lmj.ssm.mapper.OrdersMapperCustom" >
<resultMap id="OrdersPlusUser" type="UserCustom">
<!-- 用户信息-->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<!--订单信息-->
<!-- 一个用户多个订单,用collection-->
<collection property="ordersList" ofType="OrdersCustom">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!--订单明细
一个订单多个明细-->
<collection property="orderDetails" ofType="Orderdetail">
<id column="orderdetail_id" property="id"/>
<result column="items_num" property="itemsNum"/>
<result column="items_id" property="itemsId"/>
<!--一个订单明细对应一个商品-->
<association property="items" javaType="Items">
<id column="items_id" property="id"/>
<result column="detail" property="detail"/>
<result column="name" property="name"/>
</association>
</collection>
</collection>
</resultMap>
<!--查询用户和购买的商品信息-->
<select id="findOrdersPlusUser" resultMap="OrdersPlusUser">
select orders.*,`user`.username,`user`.sex,`user`.address,
orderdetail.id orderdetail_id,orderdetail.items_id,
orderdetail.items_num,
items.name,items.detail
from orders,user,orderdetail,items
where orders.user_id=user.id and orderdetail.orders_id=orders.id and orderdetail.items_id=items.id
</select>
</mapper>
jdbc.properties:
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
log4j.properties:
log4j.rootLogger=DEBUG,Console
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
findOrdersPlusUse 方法的测试类OrdersMapperCustomImplTest 代码:
package cn.itcast.ssm.dao;
import cn.itcast.ssm.po.OrdersCustom;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* OrdersMapperCustomImpl Tester.
*
* @author <Authors name>
* @since <pre>ʮһ�� 8, 2018</pre>
* @version 1.0
*/
public class OrdersMapperCustomImplTest {
private ApplicationContext applicationContext;
@Before
public void before() throws Exception {
applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@After
public void after() throws Exception {
}
/**
*
* Method: findOrdersPlusUser()
*
*/
@Test
public void testFindOrdersPlusUser() throws Exception {
//TODO: Test goes here...
OrdersMapperCustom ordersMapperCustom= (OrdersMapperCustom) applicationContext.getBean("ordersMapperCustom");
List<OrdersCustom> orderslist= ordersMapperCustom.findOrdersPlusUser();
System.out.println(orderslist);
System.out.println(orderslist);
}
}