在之前的文章中总结了三种方式,但是有两种是注解sql的,这种方式比较混乱所以大家不怎么使用,下面总结一下常用的两种总结方式:
一、 动态代理实现 不用写dao的实现类
这种方式比较简单,不用实现dao层,只需要定义接口就可以了,这里只是为了记录配置文件所以程序写的很简单:
1、整体结构图:
2、三个配置文件以及一个映射文件
(1)、程序入口以及前端控制器配置 web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<?xml version= "1.0" encoding= "utf-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee"
xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "webapp_id"
version= "3.0" >
<display-name>website1</display-name>
<!-- 设置监听,在web容器启动时自动装配applicationcontext的配置信息-->
<listener>
</listener>
<!-- 设置spring容器加载配置文件路径 -->
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>
classpath:config/springmvc-servlet.xml,
classpath:config/applicationcontext.xml
</param-value>
</context-param>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encodingfilter</filter-name>
<filter- class >org.springframework.web.filter.characterencodingfilter</filter- class >
<init-param>
<param-name>encoding</param-name>
<param-value>utf- 8 </param-value>
</init-param>
<init-param>
<param-name>forceencoding</param-name>
<param-value> true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>*. do </url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class >
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:config/springmvc-servlet.xml</param-value>
</init-param>
<!-- 这个配置文件在容器启动的时候 就加载 -->
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截请求 -->
<url-pattern>*. do </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file> default .html</welcome-file>
<welcome-file> default .htm</welcome-file>
<welcome-file> default .jsp</welcome-file>
</welcome-file-list>
</web-app>
|
(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?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:aop= "http://www.springframework.org/schema/aop" xmlns:context= "http://www.springframework.org/schema/context"
xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:util= "http://www.springframework.org/schema/util"
xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:jdbc= "http://www.springframework.org/schema/jdbc"
xmlns:cache= "http://www.springframework.org/schema/cache"
xsi:schemalocation="
http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http: //www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http: //www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- <context:annotation-config /> -->
<!-- context:component-scan 具有annotation-config 的功能 -->
<!-- 扫描 控制层 -->
<context:component-scan base- package = "com.website.controller" ></context:component-scan>
<!-- 视图解析器 -->
<bean id= "viewresolver" class = "org.springframework.web.servlet.view.internalresourceviewresolver" >
<property name= "prefix" value= "/web-inf/view/" >
</property>
<property name= "suffix" value= ".jsp" ></property>
</bean>
</beans>
|
(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionfactory 、dao层接口动态代理以及事务的配置applicationcontext.xml
这里会有多中配置文件
1)、单数据源,动态代理实现dao层接口时不设置sqlsessionfactorybeanname、或sqlsessiontemplatebeanname 两个属性的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?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:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context"
xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置jdbc文件 -->
<context:property-placeholder location= "classpath:db.properties" />
<!-- 数据源 -->
<bean id= "datasource" class = "org.springframework.jdbc.datasource.drivermanagerdatasource" >
<property name= "driverclassname" >
<value>${jdbc.driverclassname}</value>
</property>
<property name= "url" >
<value>${jdbc.url}</value>
</property>
<property name= "username" >
<value>${jdbc.username}</value>
</property>
<property name= "password" >
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 开启注解配置 即autowried -->
<!-- <context:annotation-config/> -->
<!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
<context:component-scan base- package = "com.website.service" />
<!-- 在使用mybatis时 spring使用sqlsessionfactorybean 来管理mybatis的sqlsessionfactory -->
<bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" >
<property name= "datasource" ref= "datasource" />
<!-- mybatis配置文件路径 -->
<property name= "configlocation" value= "" />
<!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
<property name= "mapperlocations" value= "classpath:mybatis/usermapper.xml" />
</bean>
<!-- <bean id= "sqlsession" class = "org.mybatis.spring.sqlsessiontemplate" > <constructor-arg index= "0" > <ref bean= "sqlsessionfactory" />
</constructor-arg> </bean> -->
<!--动态代理实现 不用写dao的实现 -->
<bean id= "mapperscannerconfigurer" class = "org.mybatis.spring.mapper.mapperscannerconfigurer" >
<!-- 这里的basepackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
<property name= "basepackage" value= "com.website.dao" />
<!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
<!-- <property name= "sqlsessionfactorybeanname" value= "sqlsessionfactory" /> -->
<!--直接指定了sqlsessiontemplate名称,这个和上面的其实是一样的 -->
<!-- <property name= "sqlsessiontemplatebeanname" value= "sqlsession" /> -->
</bean>
<!--事务管理器 -->
<bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >
<property name= "datasource" ref= "datasource" />
</bean>
<!-- 使用全注释事务 -->
<tx:annotation-driven transaction-manager= "transactionmanager" />
</beans>
|
2)、单数据源配置 sqlsessionfactorybeanname 这个属性值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?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:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context"
xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置jdbc文件 -->
<context:property-placeholder location= "classpath:db.properties" />
<!-- 数据源 -->
<bean id= "datasource" class = "org.springframework.jdbc.datasource.drivermanagerdatasource" >
<property name= "driverclassname" >
<value>${jdbc.driverclassname}</value>
</property>
<property name= "url" >
<value>${jdbc.url}</value>
</property>
<property name= "username" >
<value>${jdbc.username}</value>
</property>
<property name= "password" >
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 开启注解配置 即autowried -->
<!-- <context:annotation-config/> -->
<!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
<context:component-scan base- package = "com.website.service" />
<!-- 在使用mybatis时 spring使用sqlsessionfactorybean 来管理mybatis的sqlsessionfactory -->
<bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" >
<property name= "datasource" ref= "datasource" />
<!-- mybatis配置文件路径 -->
<property name= "configlocation" value= "" />
<!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
<property name= "mapperlocations" value= "classpath:mybatis/usermapper.xml" />
</bean>
<!-- <bean id= "sqlsession" class = "org.mybatis.spring.sqlsessiontemplate" > <constructor-arg index= "0" > <ref bean= "sqlsessionfactory" />
</constructor-arg> </bean> -->
<!--动态代理实现 不用写dao的实现 -->
<bean id= "mapperscannerconfigurer" class = "org.mybatis.spring.mapper.mapperscannerconfigurer" >
<!-- 这里的basepackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
<property name= "basepackage" value= "com.website.dao" />
<!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
<property name= "sqlsessionfactorybeanname" value= "sqlsessionfactory" />
<!--直接制定了sqlsessiontemplate名称,这个和上面的其实是一样的 -->
<!-- <property name= "sqlsessiontemplatebeanname" value= "sqlsession" /> -->
</bean>
<!--事务管理器 -->
<bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >
<property name= "datasource" ref= "datasource" />
</bean>
<!-- 使用全注释事务 -->
<tx:annotation-driven transaction-manager= "transactionmanager" />
</beans>
|
3)、单数据源配置sqlsessiontemplatebeanname 这个属性值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?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:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context"
xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置jdbc文件 -->
<context:property-placeholder location= "classpath:db.properties" />
<!-- 数据源 -->
<bean id= "datasource" class = "org.springframework.jdbc.datasource.drivermanagerdatasource" >
<property name= "driverclassname" >
<value>${jdbc.driverclassname}</value>
</property>
<property name= "url" >
<value>${jdbc.url}</value>
</property>
<property name= "username" >
<value>${jdbc.username}</value>
</property>
<property name= "password" >
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 开启注解配置 即autowried -->
<!-- <context:annotation-config/> -->
<!--其实component-scan 就有了annotation-config的功能即把需要的类注册到了spring容器中 -->
<context:component-scan base- package = "com.website.service" />
<!-- 在使用mybatis时 spring使用sqlsessionfactorybean 来管理mybatis的sqlsessionfactory -->
<bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" >
<property name= "datasource" ref= "datasource" />
<!-- mybatis配置文件路径 -->
<property name= "configlocation" value= "" />
<!-- 实体类映射文件路径,这里只有一个就写死了,多个可以使用mybatis/*.xml来替代 -->
<property name= "mapperlocations" value= "classpath:mybatis/usermapper.xml" />
</bean>
<bean id= "sqlsession" class = "org.mybatis.spring.sqlsessiontemplate" >
<constructor-arg index= "0" >
<ref bean= "sqlsessionfactory" />
</constructor-arg>
</bean>
<!--动态代理实现 不用写dao的实现 -->
<bean id= "mapperscannerconfigurer" class = "org.mybatis.spring.mapper.mapperscannerconfigurer" >
<!-- 这里的basepackage 指定了dao层接口路劲,这里的dao接口不用自己实现 -->
<property name= "basepackage" value= "com.website.dao" />
<!-- 如果只有一个数据源的话可以不用指定,但是如果有多个数据源的话必须要指定 -->
<!-- <property name= "sqlsessionfactorybeanname" value= "sqlsessionfactory" /> -->
<!--直接制定了sqlsessiontemplate名称,这个和上面的其实是一样的 -->
<property name= "sqlsessiontemplatebeanname" value= "sqlsession" />
</bean>
<!--事务管理器 -->
<bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >
<property name= "datasource" ref= "datasource" />
</bean>
<!-- 使用全注释事务 -->
<tx:annotation-driven transaction-manager= "transactionmanager" />
</beans>
|
4)、多数据源
注意如果是多数据源则一定要使用sqlsessionfactorybeanname 或sqlsessiontemplatebeanname 来指定具体的数据源,不知道在上面的配置中有没有注意到,如果使用sqlsessiontemplatebeanname 的话要
1
2
3
4
5
|
<bean id= "sqlsession" class = "org.mybatis.spring.sqlsessiontemplate" >
<constructor-arg index= "0" >
<ref bean= "sqlsessionfactory" />
</constructor-arg>
</bean>
|
来创建具体的实例并赋值给sqlsessiontemplatebeanname 这个属性。
(4)、mybatis sql映射文件 usermapper.xml:
1
2
3
4
5
6
7
8
9
10
|
<?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" >
<!-- namespace的值就是dao接口的完整路劲,就这个demo而言namespace 就是userdao.java的完整路劲 -->
<mapper namespace= "com.website.dao.userdao" >
<!-- 这里的id就是接口中方法的名称 -->
<insert id= "saveuser" parametertype= "java.util.map" >
insert into user(id,name) values(#{id},#{name})
</insert>
</mapper>
|
ok 到这里配置文件到搞定了下面来看看控制层,业务逻辑层以及dao层的代码。
3、controller层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.website.controller;
import java.util.hashmap;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import com.website.service.userservice;
@controller
@requestmapping (value = "/user" )
public class usercontroller {
// 注入userservice 对象
@autowired
private userservice userservice;
@requestmapping (value = "/save.do" , method = requestmethod.get)
public string saveuser(httpservletrequest request,
httpservletresponse response) {
string id = request.getparameter( "id" );
string name = request.getparameter( "name" );
map<string, string> map = new hashmap<string, string>();
map.put( "id" , id);
map.put( "name" , name);
userservice.saveuser(map);
return "index" ;
}
}
|
4、service层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.website.service;
import java.util.map;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import com.website.dao.userdao;
@service ( "userservice" )
@transactional
public class userservice {
// 注入dao接口实现类实例
// @resource、@autowired两种注入方式都可以
@autowired
private userdao userdao;
public void saveuser(map<string, string> map) {
int end = userdao.saveuser(map);
system.out.println( "end:" + end);
}
}
|
5、dao 层 接口
1
2
3
4
5
6
|
package com.website.dao;
import java.util.map;
//com.website.dao.userdao
public interface userdao {
int saveuser(map<string, string> map);
}
|
dao 接口的完整路劲就是这个dao 接口对应的那个映射文件的namespace 而方法名就是 id的值
ok到这里这种配置方式都完了,也有了一个完整的小demo,下面我们简单总结一下:
这种配置方式相比之前的配置方式(下面也会写出来)特别之处就是他使用了dao层接口的动态代理方式实现了,之前我们会在dao层自己手动实现dao层然后自动注入sqlsessiontemplate 实例来调用具体的方法 比如 insert("","") selectone("","") 等方法 其中第一个参数就是映射文件的地址: namespace+id 而第二个参数就是传递的条件这样mybatis 就会按照我们传递的这两个参数找到具体的映射文件进行解析查询。而这里使用动态代理就省去了我们实现dao接口的这一步骤,而是由spring提我们实现了,那有个问题,查询条件参数我们传递了,但映射文件的具体路径即:namespce+id 没有传递怎么办,那就是你的映射文件的namespace 必须是接口的类全名称而id 必须是接口中的方法名称,这样动态代理就能找到路劲了也有了参数了。 这样一来是不是觉得就一样了啊哈哈哈!
二、手动实现dao层接口
下面先来看看手动实现dao层的配置以及代码:
1、正题结构图
2、三个配置文件以及映射文件
(1)、程序入口,前端控制器配置 web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
<?xml version= "1.0" encoding= "utf-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
xmlns= "http://java.sun.com/xml/ns/javaee"
xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id= "webapp_id" version= "3.0" >
<display-name>website2</display-name>
<!-- 加载spring容器配置 -->
<listener>
<listener- class >org.springframework.web.context.contextloaderlistener</listener- class >
</listener>
<!-- 设置spring容器加载配置文件路径 -->
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>
classpath:config/springmvc-servlet.xml,
classpath:config/applicationcontext.xml
</param-value>
</context-param>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encodingfilter</filter-name>
<filter- class >org.springframework.web.filter.characterencodingfilter</filter- class >
<init-param>
<param-name>encoding</param-name>
<param-value>utf- 8 </param-value>
</init-param>
<init-param>
<param-name>forceencoding</param-name>
<param-value> true </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>*. do </url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet- class >org.springframework.web.servlet.dispatcherservlet</servlet- class >
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath:config/springmvc-servlet.xml</param-value>
</init-param>
<!-- 这个配置文件在容器启动的时候 就加载 -->
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 拦截请求 -->
<url-pattern>*. do </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file> default .html</welcome-file>
<welcome-file> default .htm</welcome-file>
<welcome-file> default .jsp</welcome-file>
</welcome-file-list>
</web-app>
|
(2)、扫描控制层、自动注入以及视图解析器的配置 springmvc-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?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:aop= "http://www.springframework.org/schema/aop"
xmlns:context= "http://www.springframework.org/schema/context"
xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:util= "http://www.springframework.org/schema/util"
xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:jdbc= "http://www.springframework.org/schema/jdbc"
xmlns:cache= "http://www.springframework.org/schema/cache"
xsi:schemalocation="
http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http: //www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http: //www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http: //www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- <context:annotation-config /> -->
<!-- 扫描 -->
<context:component-scan base- package = "com.website.controller" ></context:component-scan>
<!-- 视图解析器 -->
<bean id= "viewresolver"
class = "org.springframework.web.servlet.view.internalresourceviewresolver" >
<property name= "prefix" value= "/web-inf/view/" >
</property>
<property name= "suffix" value= ".jsp" ></property>
</bean>
</beans>
|
(3)、数据源、service 自动扫描注入、spring代管mybatissqlsessionfactory 以及事务的配置applicationcontext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<?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:tx= "http://www.springframework.org/schema/tx"
xmlns:context= "http://www.springframework.org/schema/context"
xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http: //www.springframework.org/schema/context
http: //www.springframework.org/schema/context/spring-context-3.2.xsd
http: //www.springframework.org/schema/tx
http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置jdbc文件 -->
<context:property-placeholder location= "classpath:db.properties" />
<!-- 数据源 -->
<bean id= "datasource"
class = "org.springframework.jdbc.datasource.drivermanagerdatasource" >
<property name= "driverclassname" >
<value>${jdbc.driverclassname}</value>
</property>
<property name= "url" >
<value>${jdbc.url}</value>
</property>
<property name= "username" >
<value>${jdbc.username}</value>
</property>
<property name= "password" >
<value>${jdbc.password}</value>
</property>
</bean>
<!-- 开启注解配置 即autowried -->
<!--component-scan拥有 annotation-config的功能即注入需要的类到spring容器中 -->
<!--<context:annotation-config/> -->
<!--使用自动注入的时候要 添加他来扫描bean之后才能在使用的时候 -->
<context:component-scan base- package = "com.website.service ,com.website.dao" />
<!-- 在使用mybatis时 spring使用sqlsessionfactorybean 来管理mybatis的sqlsessionfactory -->
<!-- 而像这种使用接口实现的方式 是使用sqlsessiontemplate来进行操作的,他提供了一些方法 -->
<bean id= "sqlsessionfactory" class = "org.mybatis.spring.sqlsessionfactorybean" >
<property name= "datasource" ref= "datasource" />
<!-- mybatis配置文件路径 -->
<property name= "configlocation" value= "" />
<!-- 实体类映射文件路径,在开发中映射文件肯定是多个所以使用mybatis/*.xml来替代 -->
<property name= "mapperlocations" value= "classpath:mybatis/usermapping.xml" />
</bean>
<!--其实这里类的实例就是mybatis中sqlsession -->
<bean id= "sqlsession" class = "org.mybatis.spring.sqlsessiontemplate" >
<constructor-arg index= "0" >
<ref bean= "sqlsessionfactory" />
</constructor-arg>
</bean>
<bean id= "transactionmanager"
class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >
<property name= "datasource" ref= "datasource" />
</bean>
<!--使用全注释事务 -->
<tx:annotation-driven transaction-manager= "transactionmanager" />
</beans>
|
(4)、mybatis 映射文件
1
2
3
4
5
6
7
8
9
10
11
|
<?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" >
<!--这个namespace + 下面的id 就是一个完整的路径,在dao层我们写了完整的路径之后mybatis就是映射这个文件中的相关sql语句 -->
<mapper namespace= "com.website.usermapper" >
<!-- parametertype就是你接受的参数的类型, -->
<!-- 添加用户信息 -->
<insert id= "insertuser" parametertype= "java.util.map" >
insert into user(id,name,password) values(#{id},#{name},#{password})
</insert>
</mapper>
|
你可能看到了这里的映射文件的namespace +id 是自定义的而不是dao 层接口的全类名+id
3、控制层controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.website.controller;
import java.util.hashmap;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import com.website.service.userservice;
/**
* @author whd data 2016年6月5日
*/
@controller
@requestmapping (value = "/user" )
public class usercontroller {
@autowired
private userservice userservice;
@requestmapping (value = "/save.do" )
public string saveuser(httpservletrequest request,
httpservletresponse response) {
string id = request.getparameter( "id" );
string name = request.getparameter( "name" );
string password = request.getparameter( "password" );
map<string, string> map = new hashmap<string, string>();
map.put( "id" , id);
map.put( "name" , name);
map.put( "password" , password);
userservice.saveuser(map);
return "index" ;
}
}
|
4、业务逻辑层 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.website.service;
import java.util.map;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import com.website.dao.userdao;
/**
* @author whd data 2016年6月5日
*/
@service ( "userservice" )
@transactional
public class userservice {
@autowired
private userdao userdao;
public void saveuser(map<string, string> map) {
userdao.saveuser(map);
}
}
|
5、dao层
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.website.dao;
import java.util.map;
import org.mybatis.spring.sqlsessiontemplate;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.repository;
/**
* @author whd data 2016年6月5日
*/
@repository ( "userdao" )
public class userdao {
@autowired
private sqlsessiontemplate sqlsession;
public void saveuser(map<string, string> map) {
int end = sqlsession.insert( "com.website.usermapper.insertuser" , map);
system.out.println( "end" + end);
}
}
|
我们看倒dao层的 sqlsessiontemplate 这个其实是mybatis中的sqlsession 对象,我们看到在applicationcontext.xml 中配置了,所以在我们使用时spring会帮我们自动注入,我们直接使用就可以了不用去自己创建,这也就是所谓的控制反转。ok 到此两种文件的配置方式就结束了。
总结
以上所述是小编给大家介绍的spring、mybatis 配置方式详解(常用两种方式),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/QH_JAVA/article/details/51601139