如何学习spring,springmvc面试题和mybatis源码

MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql - 黎明你好的技术博客 - ITeye博客
博客分类:
一、MyBatis简介与配置MyBatis+Spring+MySql
1.1MyBatis简介
MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spring结合包)。
下载地址:
1.2MyBatis+Spring+MySql简单配置
1.2.1搭建Spring环境
1,建立maven的web项目;2,加入Spring框架、配置文件;3,在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);4,更改web.xml和spring的配置文件;5,添加一个jsp页面和对应的Controller;6,测试。
可参照:。
1.2.2建立MySql数据库
建立一个学生选课管理数据库。表:学生表、班级表、教师表、课程表、学生选课表。逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;
使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。
更多sql请下载项目源文件,在resource/sql中。
/* 建立数据库 */
CREATE DATABASE STUDENT_MANAGER;
USE STUDENT_MANAGER;
/***** 建立student表 *****/
CREATE TABLE STUDENT_TBL
STUDENT_ID
VARCHAR(255) PRIMARY KEY,
STUDENT_NAME
VARCHAR(10) NOT NULL,
STUDENT_SEX
VARCHAR(10),
STUDENT_BIRTHDAY
VARCHAR(255)
/*插入学生数据*/
INSERT INTO STUDENT_TBL (STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
创建连接MySql使用的配置文件mysql.properties。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8
1.2.3搭建MyBatis环境
顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。
1.2.3.1创建实体类: StudentEntity
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 3606831L;
private ClassEntity classE
private Date studentB
private String studentID;
private String studentN
private String studentS
public ClassEntity getClassEntity() {
return classE
public Date getStudentBirthday() {
return studentB
public String getStudentID() {
return studentID;
public String getStudentName() {
return studentN
public String getStudentSex() {
return studentS
public void setClassEntity(ClassEntity classEntity) {
this.classEntity = classE
public void setStudentBirthday(Date studentBirthday) {
this.studentBirthday = studentB
public void setStudentID(String studentID) {
this.studentID = studentID;
public void setStudentName(String studentName) {
this.studentName = studentN
public void setStudentSex(String studentSex) {
this.studentSex = studentS
1.2.3.2创建数据访问接口
Student类对应的dao接口:StudentMapper。
public interface StudentMapper {
public StudentEntity getStudent(String studentID);
public StudentEntity getStudentAndClass(String studentID);
public List&StudentEntity& getStudentAll();
public void insertStudent(StudentEntity entity);
public void deleteStudent(StudentEntity entity);
public void updateStudent(StudentEntity entity);
1.2.3.3创建SQL映射语句文件
Student类的sql语句文件StudentMapper.xmlresultMap标签:表字段与属性的映射。Select标签:查询sql。
&?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.manager.data.StudentMapper"&
&resultMap type="StudentEntity" id="studentResultMap"&
&id property="studentID" column="STUDENT_ID"/&
&result property="studentName" column="STUDENT_NAME"/&
&result property="studentSex" column="STUDENT_SEX"/&
&result property="studentBirthday" column="STUDENT_BIRTHDAY"/&
&/resultMap&
&!-- 查询学生,根据id --&
&select id="getStudent" parameterType="String" resultType="StudentEntity" resultMap="studentResultMap"&
SELECT * from STUDENT_TBL ST
WHERE ST.STUDENT_ID = #{studentID}
&!-- 查询学生列表 --&
&select id="getStudentAll"
resultType="com.manager.data.model.StudentEntity" resultMap="studentResultMap"&
SELECT * from STUDENT_TBL
1.2.3.4创建MyBatis的mapper配置文件
在src/main/resource中创建MyBatis配置文件:mybatis-config.xml。typeAliases标签:给类起一个别名。com.manager.data.model.StudentEntity类,可以使用StudentEntity代替。Mappers标签:加载MyBatis中实体类的SQL映射语句文件。
&?xml version="1.0" encoding="UTF-8" ?&
&!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"&
&configuration&
&typeAliases&
&typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/&
&/typeAliases&
&mapper resource="com/manager/data/maps/StudentMapper.xml" /&
&/mappers&
&/configuration&
1.2.3.5修改Spring 的配置文件
主要是添加SqlSession的制作工厂类的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。和数据访问接口对应的实现bean。通过MapperFactoryBean创建出来。需要执行接口类全称和SqlSession工厂bean的引用。
&!-- 导入属性配置文件 --&
&context:property-placeholder location="classpath:mysql.properties" /&
&bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&
&property name="driverClassName" value="${jdbc.driverClassName}" /&
&property name="url" value="${jdbc.url}" /&
&bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&
&property name="dataSource" ref="dataSource" /&
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation" value="classpath:mybatis-config.xml" /&
&property name="dataSource" ref="dataSource" /&
&!— mapper bean --&
&bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"&
&property name="mapperInterface" value="com.manager.data.StudentMapper" /&
&property name="sqlSessionFactory" ref="sqlSessionFactory" /&
也可以不定义mapper的bean,使用注解:
将StudentMapper加入注解
@Repository
@Transactional
public interface StudentMapper {
对应的需要在dispatcher-servlet.xml中加入扫描:
&bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&
&property name="annotationClass" value="org.springframework.stereotype.Repository"/&
&property name="basePackage" value="com.liming.manager"/&
&property name="sqlSessionFactory" ref="sqlSessionFactory"/&
1.2.4测试StudentMapper
使用SpringMVC测试,创建一个TestController,配置tomcat,访问index.do页面进行测试:
@Controller
public class TestController {
@Autowired
private StudentMapper studentM
@RequestMapping(value = "index.do")
public void indexPage() {
StudentEntity entity = studentMapper.getStudent("");
System.out.println("name:" + entity.getStudentName());
使用Junit测试:
使用Junit测试:
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "test-servlet.xml")
public class StudentMapperTest {
@Autowired
private ClassMapper classM
@Autowired
private StudentMapper studentM
@Transactional
public void getStudentTest(){
StudentEntity entity = studentMapper.getStudent("");
System.out.println("" + entity.getStudentID() + entity.getStudentName());
List&StudentEntity& studentList = studentMapper.getStudentAll();
for( StudentEntity entityTemp : studentList){
System.out.println(entityTemp.getStudentName());
62 顶11 踩
浏览 284998
&bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"&& &&& &property name="mapperInterface" value="com.manager.data.StudentMapper" /&& &&& &property name="sqlSessionFactory" ref="sqlSessionFactory" /&& MapperFactoryBean的路径写错了,应该是org.mybatis.spring.mapper.MapperFactoryBean
hao_evolution 写道为啥加两次 @Transactional ,在service层加不就可以了?你说的两次,第二次指的是在junit里边吧。在junit里面加@Transactional可以使在测试增删改后进行数据回滚,保证数据和测试之前的一样,不加也没问题。@Repository& @Transactional& public interface StudentMapper {& } 个人觉得这里没必要加事务,在service层去加事务控制
为啥加两次 @Transactional ,在service层加不就可以了?你说的两次,第二次指的是在junit里边吧。在junit里面加@Transactional可以使在测试增删改后进行数据回滚,保证数据和测试之前的一样,不加也没问题。
xiaojianhx 写道事务怎么加?哦,写错了.....是"事务怎么处理"使用Spring的@Transactional注解,一般写在Service层的接口方法上。
事务怎么加?哦,写错了.....是"事务怎么处理"
xiaojianhx 写道我eclipse 启动tomcat的时候报错,怎么回事啊
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initializ nested exception is java.lang.reflect.MalformedParameterizedTypeException
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation"&
&value&classpath:mybatis-config.xml&/value&
&/property&
&property name="dataSource"&
&ref bean="dataSource" /&
&/property&
遇到相同的问题,调试了一晚上没有找到原因,不知7楼是否已经解决该问题了?若解决了,请不吝赐教!
终于弄明白了,原来是spring版本的问题,MyBatis-Spring只能在spring3.0版本上使用,如果用spring3.0以下版本就会出现该问题,建议将spring升级为3.0或改用ibatis2.0!
我eclipse 启动tomcat的时候报错,怎么回事啊
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Initializ nested exception is java.lang.reflect.MalformedParameterizedTypeException
&bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
&property name="configLocation"&
&value&classpath:mybatis-config.xml&/value&
&/property&
&property name="dataSource"&
&ref bean="dataSource" /&
&/property&
遇到相同的问题,调试了一晚上没有找到原因,不知7楼是否已经解决该问题了?若解决了,请不吝赐教!
不好意思,你的typeAliases,在哪里用到了....
在SQL映射语句文件中,使用的实体类(Java类型声明时),如果没有定义这个&typeAliases&,那么我们用到实体类都需要写带包名的全称:com.manager.data.model.StudentEntity。而使用typeAliases就给这个类起了一个名字,也就是简称:StudentEntity,所以以后在使用这个实体类的时候,我们只需要写StudentEntity,而不用写带包名的全称。
& 上一页 1
limingnihao
浏览: 1531324 次
来自: 北京
好详细,写的真全面
哪里有源代码啊,?能否发一份?还有就是 ClassEntity ...
classentity是啥?哪个包的类啊?这教程并不完整啊!
shift+alt+a
严重: Exception starting filt ...spring(10)
springmvc(2)
Mybatis(4)
注:本文参考
参考项目地址:
本人项目地址:
seckill(Java高并发秒杀API)
慕课网视频
Java高并发秒杀系统API
慕课网秒杀系统的视频教程重新总结了自己使用SSM开发项目的经历。
IDEA+Maven+SSM框架。
用maven对项目进行管理的知识很简单,关于创建maven项目的知识大家可以去慕课网学习一下
秒杀系统搭建环境:IDEA+Maven+SSM框架。
完成这个秒杀系统,需要完成四个模块的代码编写,分别是:
其实完成这三个模块就可以完成我们的秒杀系统了,但对于我们的秒杀系统中一件秒杀商品,在秒杀的时候肯定会有成千上万的用户参与进来,通过上述三个模块完成的系统无法解决这么多用户的高并发操作,所以我们还需要第四个模块:
本篇文章进行第一个模块的讲解及项目的介绍以及Dao层编码的开发。首先看看我们项目的效果图:
接下来我将从如何用maven创建我们的秒杀系统seckill项目开始到完成我们的秒杀系统,详细介绍自己完成它的过程。
1.相关技术介绍
**MySQL:**1.这里我们采用手写代码创建相关表,掌握这种能力对我们以后的项目二次上线会有很大的帮助;2.SQL技巧;3.事务和行级锁的理解和一些应用。
**MyBatis:**1.DAO层的设计与开发。2.MyBatis的合理使用,使用Mapper动态代理的方式进行数据库的访问。3.MyBatis和Spring框架的整合:如何高效的去整合MyBatis和Spring框架。
**Spring:**1.Spring IOC帮我们整合Service以及Service所有的依赖。2.声明式事务。对Spring声明式事务做一些分析以及它的行为分析。
**Spring MVC:**1.Restful接口设计和使用。Restful现在更多的被应用在一些互联网公司Web层接口的应用上。2.框架运作流程。3.Spring Controller的使用技巧。
**前端:**1.交互设计。2.bootstrap。3.JQuery。设计到前端的页面代码我们直接拷贝即可,毕竟真正开发中这样一个项目是由产品经理、前端工程师、后端工程师一起完成的。
**高并发:**1.高并发点和高并发分析。2.优化思路并实现。
下面开始我们的项目的开发。
2.Java高并发秒杀APi之业务分析与DAO层代码编写
2.1用Maven创建我们的项目seckill
在命令行中输入如下命令:
mvn archetype:generate -DgroupId=cn.codingxiaxw.seckill -DartifactId=seckill -Dpackage=cn.codingxiaxw.seckill -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp
然后使用IDEA打开该项目,在IDEA中对项目按照Maven项目的标准骨架补全我们项目的相应文件包,最后的工程结构如下:
main包下进行我们项目的代码编写及相关配置文件,test包下进行我们项目的测试。
打开WEB-INF下的web.xml,它默认为我们创建servlet版本为2.3,需要修改它的根标签为:
xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true"&
然后打开pom.xml,在里面添加我们需要的第三方jar包的坐标配置信息,如SSM框架、数据库、日志,如下:
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&
&cn.codingxiaxw.seckill&
&1.0-SNAPSHOT&
&seckill Maven Webapp&
&http://maven.apache.org&
&org.slf4j&
&slf4j-api&
&ch.qos.logback&
&logback-core&
&ch.qos.logback&
&logback-classic&
&mysql-connector-java&
&org.mybatis&
&org.mybatis&
&mybatis-spring&
&standard&
&com.fasterxml.jackson.core&
&jackson-databind&
&javax.servlet&
&javax.servlet-api&
&org.springframework&
&spring-core&
&4.1.7.RELEASE&
&org.springframework&
&spring-beans&
&4.1.7.RELEASE&
&org.springframework&
&spring-context&
&4.1.7.RELEASE&
&org.springframework&
&spring-jdbc&
&4.1.7.RELEASE&
&org.springframework&
&spring-tx&
&4.1.7.RELEASE&
&org.springframework&
&spring-web&
&4.1.7.RELEASE&
&org.springframework&
&spring-webmvc&
&4.1.7.RELEASE&
&org.springframework&
&spring-test&
&4.1.7.RELEASE&
到此,我们项目的初始化工作完成。
2.2秒杀系统业务分析
秒杀系统业务流程如下:
由图可以发现,整个系统其实是针对库存做的系统。用户成功秒杀商品,对于我们系统的操作就是:1.减库存。2.记录用户的购买明细。下面看看我们用户对库存的业务分析:
记录用户的秒杀成功信息,我们需要记录:1.谁购买成功了。2.购买成功的时间/有效期。3.付款/发货信息。这些数据组成了用户的秒杀成功信息,也就是用户的购买行为。
为什么我们的系统需要事务?看如下这些故障:1.若是用户成功秒杀商品我们记录了其购买明细却没有减库存。导致商品的超卖。2.减了库存却没有记录用户的购买明细。导致商品的少卖。对于上述两个故障,若是没有事务的支持,损失最大的无疑是我们的用户和商家。在MySQL中,它内置的事务机制,可以准确的帮我们完成减库存和记录用户购买明细的过程。
MySQL实现秒杀的难点分析:当用户A秒杀id为10的商品时,此时MySQL需要进行的操作是:1.开启事务。2.更新商品的库存信息。3.添加用户的购买明细,包括用户秒杀的商品id以及唯一标识用户身份的信息如电话号码等。4.提交事务。若此时有另一个用户B也在秒杀这件id为10的商品,他就需要等待,等待到用户A成功秒杀到这件商品然后MySQL成功的提交了事务他才能拿到这个id为10的商品的锁从而进行秒杀,而同一时间是不可能只有用户B在等待,肯定是有很多很多的用户都在等待拿到这个行级锁。秒杀的难点就在这里,如何高效的处理这些竞争?如何高效的完成事务?在后面第4个模块如何进行高并发的优化为大家讲解。
我们这个系统需要完成秒杀的哪些功能?先来看看天猫的一个秒杀库存系统:
大家看了是不是觉得很复杂?当然不用担心,我们只是实现秒杀的一些功能:1.秒杀接口的暴露。2.执行秒杀的操作。3.相关查询,比如说列表查询,详情页查询。我们实现这三个功能即可。接下来进行具体的编码工作,首先是Dao层的编码。
2.3Dao层设计开发
首先创建数据库,相关表的sql语句我在main包下的sql包中已经给出。
然后创建对应表的实体类,在java包下创建cn.codingxiaxw.entity包,创建一个Seckill.java实体类,代码如下:
public class Seckill
private long seckillId;
private int
private Date startT
private Date endT
private Date createT
public long getSeckillId() {
return seckillId;
public void setSeckillId(long seckillId) {
this.seckillId = seckillId;
public String getName() {
public void setName(String name) {
this.name =
public int getNumber() {
public void setNumber(int number) {
this.number =
public Date getStartTime() {
return startT
public void setStartTime(Date startTime) {
this.startTime = startT
public Date getEndTime() {
return endT
public void setEndTime(Date endTime) {
this.endTime = endT
public Date getCreateTime() {
return createT
public void setCreateTime(Date createTime) {
this.createTime = createT
public String toString() {
return "Seckill{" +
"seckillId=" + seckillId +
", name='" + name + '\'' +
", number=" + number +
", startTime=" + startTime +
", endTime=" + endTime +
", createTime=" + createTime +
和一个SuccessKilled.java,代码如下:
public class SuccessKilled
private long seckillId;
private long userP
private short
private Date createT
public long getSeckillId() {
return seckillId;
public void setSeckillId(long seckillId) {
this.seckillId = seckillId;
public long getUserPhone() {
return userP
public void setUserPhone(long userPhone) {
this.userPhone = userP
public short getState() {
public void setState(short state) {
this.state =
public Date getCreateTime() {
return createT
public void setCreateTime(Date createTime) {
this.createTime = createT
public Seckill getSeckill() {
public void setSeckill(Seckill seckill) {
this.seckill =
public String toString() {
return "SuccessKilled{" +
"seckillId=" + seckillId +
", userPhone=" + userPhone +
", state=" + state +
", createTime=" + createTime +
然后针对实体创建出对应dao层的接口,在cn.codingxiaxw.dao包下创建Seckill.java:
public interface SeckillDao
* seckillId
* killTime
* 如果影响行数&1,表示更新库存的记录行数
int reduceNumber(long seckillId, Date killTime);
* 根据id查询秒杀的商品信息
* seckillId
Seckill queryById(long seckillId);
* 根据偏移量查询秒杀商品列表
List&Seckill& queryAll(int off,int limit);
和SuccessKilled.java:
public interface SuccessKilledDao {
* 插入购买明细,可过滤重复
* seckillId
* userPhone
*插入的行数
int insertSuccessKilled(long seckillId,long userPhone);
* 根据秒杀商品的id查询明细SuccessKilled对象(该对象携带了Seckill秒杀产品对象)
* seckillId
SuccessKilled queryByIdWithSeckill(long seckillId,long userPhone);
接下来基于MyBatis来实现我们之前设计的Dao层接口。首先需要配置我们的MyBatis,在resources包下创建MyBatis全局配置文件mybatis-config.xml文件,在浏览器中输入http://mybatis.github.io/mybatis-3/zh/index.html打开MyBatis的官网文档,点击左边的”入门”栏框,找到mybatis全局配置文件,在这里有xml的一个规范,也就是它的一个xml约束,拷贝:
&?xml version="1.0" encoding="UTF-8" ?&
&!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"&
到我们的项目mybatis全局配置文件中,然后在全局配置文件中加入如下配置信息:
&?xml version="1.0" encoding="UTF-8" ?&
&!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"&
name="useGeneratedKeys" value="true"/&
name="useColumnLabel" value="true"/&
name="mapUnderscoreToCamelCase" value="true"/&
配置文件创建好后我们需要关注的是Dao接口该如何实现,mybatis为我们提供了mapper动态代理开发的方式为我们自动实现Dao的接口。在mapper包下创建对应Dao接口的xml映射文件,里面用于编写我们操作数据库的sql语句,SeckillDao.xml和SuccessKilledDao.xml。既然又是一个xml文件,我们肯定需要它的dtd文件,在官方文档中,点击左侧”XML配置”,在它的一些事例中,找到它的xml约束:
&!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&
加入到两个mapper映射xml文件中,然后对照Dao层方法编写我们的映射文件内容如下:
SeckillDao.xml:
&!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&
namespace="cn.codingxiaxw.dao.SeckillDao"&
id="reduceNumber"&
UPDATE seckill
SET number = number-1
WHERE seckill_id=#{seckillId}
AND start_time &![CDATA[ &= ]]& #{killTime}
AND end_time &= #{killTime}
AND number & 0;
id="queryById" resultType="Seckill" parameterType="long"&
FROM seckill
WHERE seckill_id=#{seckillId}
id="queryAll" resultType="Seckill"&
FROM seckill
ORDER BY create_time DESC
limit #{offset},#{limit}
SuccessKilledDao.xml:
&!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&
namespace="cn.codingxiaxw.dao.SuccessKilledDao"&
id="insertSuccessKilled"&
INSERT ignore INTO success_killed(seckill_id,user_phone,state)
VALUES (#{seckillId},#{userPhone},0)
id="queryByIdWithSeckill" resultType="SuccessKilled"&
sk.seckill_id,
sk.user_phone,
sk.create_time,
s.seckill_id "seckill.seckill_id",
s.name "seckill.name",
s.number "seckill",
s.start_time "seckill.start_time",
s.end_time "seckill.end_time",
s.create_time "seckill.create_time"
FROM success_killed sk
INNER JOIN seckill s ON sk.seckill_id=s.seckill_id
WHERE sk.seckill_id=#{seckillId}
AND sk.user_phone=#{userPhone}
接下来我们开始MyBatis和Spring的整合,整合目标:1.更少的编码:只写接口,不写实现类。2.更少的配置:别名、配置扫描映射xml文件、dao实现。3.足够的灵活性:自由定制SQL语句、自由传结果集自动赋值。
在resources包下创建一个spring包,里面放置spring对Dao、Service、transaction的配置文件。在浏览器中输入http://docs.spring.io/spring/docs/进入到Spring的官网中下载其pdf官方文档,在其官方文档中找到它的xml的定义内容头部:
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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"&
在spring包下创建一个spring配置dao层对象的配置文件spring-dao.xml,加入上述dtd约束,然后添加二者整合的配置,内容如下:
&?xml version="1.0" encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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"&
location="classpath:jdbc.properties"/&
id="dataSource" class="com.mchange.boPooledDataSource"&
name="driverClass" value="${driver}" /&
name="jdbcUrl" value="${url}" /&
name="user" value="${username}" /&
name="password" value="${password}" /&
name="maxPoolSize" value="30"/&
name="minPoolSize" value="10"/&
name="autoCommitOnClose" value="false"/&
name="checkoutTimeout" value="1000"/&
name="acquireRetryAttempts" value="2"/&
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&
name="dataSource" ref="dataSource"/&
name="configLocation" value="classpath:mybatis-config.xml"/&
name="typeAliasesPackage" value="cn.codingxiaxw.entity"/&
name="mapperLocations" value="classpath:mapper/*.xml"/&
class="org.mybatis.spring.mapper.MapperScannerConfigurer"&
name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/&
name="basePackage" value="cn.codingxiaxw.dao"/&
需要我们在resources包下创建jdbc.properties用于配置数据库的连接信息,内容如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=utf-8
username=root
password=xiaxunwu1996.
这样我们便完成了Dao层编码的开发,接下来就可以利用junit进行我们Dao层编码的测试了。首先测试SeckillDao.java,利用IDEA快捷键shift+command+T对SeckillDao.java进行测试,然后IDEA会自动在test包的java包下为我们生成对SeckillDao.java中所有方法的测试类SeckillDaoTest.java,内容如下:
public class SeckillDaoTest {
public void reduceNumber() throws Exception {
public void queryById() throws Exception {
public void queryAll() throws Exception {
然后便可以在这个测试类中对SeckillDao接口的所有方法进行测试了,先测试queryById()方法,在该方法中添加内容:
* Created by codingBoy on 16/11/27.
* 配置spring和junit整合,这样junit在启动时就会加载spring容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class SeckillDaoTest {
private SeckillDao seckillD
public void queryById() throws Exception {
long seckillId=1000;
Seckill seckill=seckillDao.queryById(seckillId);
System.out.println(seckill.getName());
System.out.println(seckill);
右键选择”debug queryById()”,测试台成功输入该id为1000的商品信息,证明Dao的该方法正确,然后测试queryAll()方法,在该方法中添加如下内容:
public void queryAll() throws Exception {
List&Seckill& seckills=seckillDao.queryAll(0,100);
for (Seckill seckill : seckills)
System.out.println(seckill);
然后运行该方法,程序报错,报错信息如下:
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'offset' not found. Available parameters are [1, 0, param1, param2]
意思就是无法完成offset参数的绑定,这也是我们java编程语言的一个问题,也就是java没有保存行参的记录,java在运行的时候会把List&Seckill& queryAll(int offset,int limit);中的参数变成这样:queryAll(int arg0,int arg1),这样我们就没有办法去传递多个参数。需要在SeckillDao接口中修改方法:
List&Seckill& queryAll(@Param("offset") int offset,@Param("limit") int limit);
这样才能使我们的MyBatis识别offset和limit两个参数,将Dao层方法中的这两个参数与xml映射文件中sql语句的传入参数完成映射。然后重新测试,发现测试通过。然后测试reduceNumber()方法,在该方法中加入如下内容:
public void reduceNumber() throws Exception {
long seckillId=1000;
Date date=new Date();
int updateCount=seckillDao.reduceNumber(seckillId,date);
System.out.println(updateCount);
运行该方法,报错:
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'seckillId' not found. Available parameters are [1, 0, param1, param2]
发现依然是我们之前那个错误,更改SeckillDao接口的reduceNumber()方法:
int reduceNumber(@Param("seckillId") long seckillId, @Param("killTime") Date killTime);
然后重新运行,测试通过,可是我们查询数据库发现该库存表的商品数量没有减少,是因为我们当前时间没有达到秒杀商品要求的时间,所以不会成功秒杀。接下来进行SuccessKilledDao接口相关方法的测试,依旧使用IDEA快捷键shift+command+T快速生成其方法的相应测试类:
public class SuccessKilledDaoTest {
public void insertSuccessKilled() throws Exception
public void queryByIdWithSeckill() throws Exception
依然要在SuccessKilledDao的方法中用@Param注解完成参数的绑定,首先完成insertSuccessKilled()的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class SuccessKilledDaoTest {
private SuccessKilledDao successKilledD
public void insertSuccessKilled() throws Exception
long seckillId=1000;
long userPhone=L;
int insertCount=successKilledDao.insertSuccessKilled(seckillId,userPhone);
System.out.println("insertCount="+insertCount);
运行成功,测试台打印出insertCount=1的信息,即我们修改了表中的一条记录,这时查看秒杀成功明细表,发现该用户的信息已经被插入。然后再次运行该测试方法,程序没有报主键异常的错,是因为我们在编写我们的明细表的时候添加了一个联合主键的字段,它保证我们明细表中的seckillId和userPhone不能重复插入,另外在SuccessDao.xml中写的插入语句的ignore关键字也保证了这点。控制台输出0,表示没有对明细表做插入操作。然后进行queryByIdWithSeckill()方法的测试,需要在Dao层的方法中添加@Param注解:
SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId,@Param("userPhone") long userPhone);
然后进行该方法的测试:
public void queryByIdWithSeckill() throws Exception
long seckillId=1000L;
long userPhone=L;
SuccessKilled successKilled=successKilledDao.queryByIdWithSeckill(seckillId,userPhone);
System.out.println(successKilled);
System.out.println(successKilled.getSeckill());
运行,成功查询出我们明细表中id为1000且手机号码为的用户信息,并将表中对应的信息映射到SuccessKilled对象和Seckill对象的属性中。
到此,我们成功完成了Dao层开发及测试
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:103809次
积分:2269
积分:2269
排名:第17144名
原创:115篇
转载:74篇
评论:15条
(1)(3)(7)(8)(10)(10)(8)(22)(27)(24)(11)(4)(16)(7)(5)(7)(2)(2)(2)(1)(3)(1)(5)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'

我要回帖

更多关于 springmvc面试题 的文章

 

随机推荐