标签:
由于进行框架的整合,所以原来好些就不需要,例如mybatis的总配置文件。
框架结构:
java
com.jd.jr.contorllor
ActivityControllor.java
com.jd.jr.dao
UserMapper.java
UserMapper.xml
com.jd.jr.po
UserPo.java
com.jd.jr.service
GetOrder.java
resources
applicationContext-dao.xml
log4j.properties
spring-config.xml
springmvc-servlet.xml
webapp
WEB-INF
web.xml
各个文件的说明:
ActivityControllor:是springMVC的处理器
package com.jd.jr.controllor; import com.jd.jr.service.GetOrder; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * Created by guojiangjiang on 2015/7/23. */ @Controller public class ActivityControllor { Logger logger = Logger.getLogger(ActivityControllor.class); @Autowired public GetOrder getOrder; @RequestMapping("/hello") public ModelAndView say(){ logger.info("kaishi le..."); System.out.println("controllor..."); getOrder.getLotteryDrawMapper(); ModelAndView mv = new ModelAndView(); return mv; } public GetOrder getOrder() { return getOrder; } public void setOrder(GetOrder order) { this.getOrder = order; } }
UserMapper.java是dao层的接口:
package com.jd.jr.dao; import com.jd.jr.po.UserPo; public interface UserMapper { public UserPo findBy(String pin); }
在UserMapper的同目录下建立同名的UserMapper.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"> <!--namespace就是接口的全类名--> <mapper namespace="com.jd.jr.dao.UserMapper"> <select id="findBy" parameterType="java.lang.String" resultType="com.jd.jr.po.UserPo"> SELECT * FROM record WHERE pin =#{pin}; </select> </mapper>
userPo类
package com.jd.jr.po; import org.springframework.stereotype.Component; @Component public class UserPo { private int id; private String pin; @Override public String toString() { return "LotteryDrawRecord{" + "id=" + id + ", pin='" + pin + '\'' + '}'; } public UserPo() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPin() { return pin; } public void setPin(String pin) { this.pin = pin; } public UserPo(int id, String pin) { this.id = id; this.pin = pin; } }
service层:
package com.jd.jr.service; import com.jd.jr.dao.UserMapper; import com.jd.jr.po.UserPo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by guojiangjiang on 2015/7/24. */ @Service public class GetOrder { @Autowired public UserMapper userMapper ; public void getLotteryDrawMapper(){ System.out.println("contorllor invoke service ..."); System.out.println(userMapper.findBy("a")); } public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } }
下面就是配置文件:
首先配置web.xml,在web.xml中需要配置springMVC的dispatcherServlet,由于整合了spring,需要在web容器启动的时候进行加载spring容器。配置如下:
<?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_2_5.xsd" version="2.5"> <display-name>jd-flow-bid-web</display-name> <!--配置web容器启动时加载spring容器,徐璈指定spring容器的位置classpath:spring-config.xml--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.xml</param-value> </context-param> <!--配置监听器监听spring容器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置springMVC的dispatcherServlet--> <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:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
配置spring的核心容器spring-config.xml,这里先配置一个空的spring容器,然后在配置一个专门用于整合mybatis的容器,最后将spring-mybatis容器加载到主容器中(web.xml中配置的就是主容器)。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--自动扫描组件,拍除cotrollor在外--> <context:component-scan base-package="com.jd.jr.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /><!-- 排除注解为controller的类型 --> </context:component-scan> <import resource="applicationContext-dao.xml" /> </beans>
配置和mybatis整合的容器:applicationContext-dao.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd " default-autowire="byName" > <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="jdbc:mysql://127.0.0.1:3306/activity"/> <property name="username" value="root"/> <property name="password" value="root"/> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="3"/> <property name="minIdle" value="1"/> <property name="maxActive" value="20"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="60000"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000"/> <property name="validationQuery" value="SELECT 'x'"/> <property name="testWhileIdle" value="true"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> </bean> <!-- MyBatis配置 配置会话工厂SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 使用自动扫描mapper的方式加载mapper.xml --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描包的路径,mapper.xml的路径 --> <property name="basePackage" value="com.jd.jr.dao"></property> </bean> <!-- 事务管理器, Jdbc单数据源事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
springMVC的配置文件:springMVC-servlet.xml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" default-lazy-init="false" > <!-- 使用annotation 自动注册bean,并检查@Required,@Resource的属性已被注入--> <context:component-scan base-package="com.jd.jr.controllor"></context:component-scan> <context:annotation-config/> </beans>
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/mggwct/article/details/47044205