SSM -1.1

4.2、Spring与MyBatis的整合

所有需要的JAR包都引入以后,首先进行SpringMyBatis的整合,然后再进行JUnit测试,先看一个项目结构图:

4.2.1、建立JDBC属性文件

jdbc.properties(文件编码修改为utf-8

driver=com.<span id="19_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="19_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=mysql&k0=mysql&k1=%C9%A8%C3%E8&k2=spring&k3=mvc&k4=xml&k5=data&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=9&seller_id=1&di=128" target="_blank" mpid="19" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">mysql</span></a></span>.jdbc.Driver
url=jdbc:mysql://10.221.10.111:8080/db_zsl
username=demao
password=demao
#定义初始连接数
initialSize=0
#定义最大连接数
<span id="20_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="20_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=max&k0=max&k1=jdbc&k2=%CA%FD%BE%DD%BF%E2&k3=mysql&k4=%C9%A8%C3%E8&k5=spring&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=6&seller_id=1&di=128" target="_blank" mpid="20" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">max</span></a></span>Active=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

4.2.2、建立spring-mybatis.xml配置文件

        这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。

spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:<span id="5_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=xsi&k0=xsi&k1=max&k2=jdbc&k3=%CA%FD%BE%DD%BF%E2&k4=mysql&k5=%C9%A8%C3%E8&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=5&seller_id=1&di=128" target="_blank" mpid="5" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">xsi</span></a></span>="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:<span id="6_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=mvc&k0=mvc&k1=xml&k2=data&k3=xsi&k4=max&k5=jdbc&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="6" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">mvc</span></a></span>="http://www.<span id="7_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=spring&k0=spring&k1=mvc&k2=xml&k3=data&k4=xsi&k5=max&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=1&seller_id=1&di=128" target="_blank" mpid="7" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">spring</span></a></span>framework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/<span id="8_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=mvc&k0=mvc&k1=xml&k2=data&k3=xsi&k4=max&k5=jdbc&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="8" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">mvc</span></a></span>  
                        http://www.<span id="9_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=spring&k0=spring&k1=mvc&k2=xml&k3=data&k4=xsi&k5=max&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=1&seller_id=1&di=128" target="_blank" mpid="9" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">spring</span></a></span>framework.org/schema/mvc/spring-mvc-4.0.xsd">
	<!-- 自动<span id="10_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=%C9%A8%C3%E8&k0=%C9%A8%C3%E8&k1=spring&k2=mvc&k3=xml&k4=data&k5=xsi&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=10&seller_id=1&di=128" target="_blank" mpid="10" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">扫描</span></a></span> -->
	<context:component-scan base-package="com.cn.hnust" />
	<!-- 引入配置文件 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:<span id="11_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=jdbc&k0=jdbc&k1=%CA%FD%BE%DD%BF%E2&k2=mysql&k3=%C9%A8%C3%E8&k4=spring&k5=mvc&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=7&seller_id=1&di=128" target="_blank" mpid="11" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">jdbc</span></a></span>.properties" />
	</bean>
	<bean id="<span id="12_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=data&k0=data&k1=xsi&k2=max&k3=jdbc&k4=%CA%FD%BE%DD%BF%E2&k5=mysql&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=4&seller_id=1&di=128" target="_blank" mpid="12" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>Source" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="<span id="13_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=max&k0=max&k1=jdbc&k2=%CA%FD%BE%DD%BF%E2&k3=mysql&k4=%C9%A8%C3%E8&k5=spring&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=6&seller_id=1&di=128" target="_blank" mpid="13" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">max</span></a></span>Active" value="${maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${maxWait}"></property>
	</bean>
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="<span id="14_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=data&k0=data&k1=xsi&k2=max&k3=jdbc&k4=%CA%FD%BE%DD%BF%E2&k5=mysql&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=4&seller_id=1&di=128" target="_blank" mpid="14" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>Source" ref="dataSource" />
		<!-- 自动<span id="15_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=%C9%A8%C3%E8&k0=%C9%A8%C3%E8&k1=spring&k2=mvc&k3=xml&k4=data&k5=xsi&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=10&seller_id=1&di=128" target="_blank" mpid="15" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">扫描</span></a></span>mapping.<span id="16_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=xml&k0=xml&k1=data&k2=xsi&k3=max&k4=jdbc&k5=%CA%FD%BE%DD%BF%E2&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=3&seller_id=1&di=128" target="_blank" mpid="16" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">xml</span></a></span>文件 -->
		<property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>
	</bean>
	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.cn.hnust.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.<span id="17_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="17_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=jdbc&k0=jdbc&k1=%CA%FD%BE%DD%BF%E2&k2=mysql&k3=%C9%A8%C3%E8&k4=spring&k5=mvc&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=7&seller_id=1&di=128" target="_blank" mpid="17" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">jdbc</span></a></span>.<span id="18_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="18_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FSpring%2F318424%5F4%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=data&k0=data&k1=xsi&k2=max&k3=jdbc&k4=%CA%FD%BE%DD%BF%E2&k5=mysql&sid=5b714432c5d51dfe&ch=0&tu=u1807396&jk=1136f40c62b6ecd1&cf=29&fv=15&stid=9&urlid=0&luki=4&seller_id=1&di=128" target="_blank" mpid="18" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">data</span></a></span>source.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>

4.2.3、Log4j的配置

为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。

      Log4j的配置很简单,而且也是通用的,下面给出一个基本的配置,换到其他项目中也无需做多大的调整,如果想做调整或者想了解Log4j的各种配置,参看我转载的一篇博文,很详细:

http://blog.csdn.net/zhshulin/article/details/37937365

下面给出配置文件目录:

log4j.properties

#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
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

4.2.4、JUnit测试

      经过以上步骤(到4.2.2,log4j不配也没影响),我们已经完成了Spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。

4.2.4.1、创建测试用表

既然我们需要测试,那么我们就需要建立在数据库中建立一个测试表,这个表建的很简单,SQL语句为:

DROP TABLE IF EXISTS `user_t`;
CREATE TABLE `user_t` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(40) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `user_t` */
insert  into `user_t`(`id`,`user_name`,`password`,`age`) values (1,'测试','sfasgfaf',24);
4.2.4.2、利用MyBatis Generator自动创建代码

参考博文:http://blog.csdn.net/zhshulin/article/details/23912615

       这个可根据表自动创建实体类、MyBatis映射文件以及DAO接口,当然,我习惯将生成的接口名改为IUserDao,而不是直接用它生成的UserMapper。如果不想麻烦就可以不改。完成后将文件复制到工程中。如图:

4.2.4.3、建立Service接口和实现类

目录结构:

下面给出具体的内容:

IUserService.jave

package com.cn.hnust.service;
import com.cn.hnust.pojo.User;
public interface IUserService {
	public User getUserById(int userId);
}
UserServiceImpl.java

package com.cn.hnust.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cn.hnust.dao.IUserDao;
import com.cn.hnust.pojo.User;
import com.cn.hnust.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	@Resource
	private IUserDao userDao;
	@Override
	public User getUserById(int userId) {
		// TODO Auto-generated method stub
		return this.userDao.selectByPrimaryKey(userId);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值