码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC+mybatis

时间:2017-09-30 11:28:36      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:ati   方便   turn   odi   事务管理   val   tar   figure   url   

 

xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-*.xml</param-value>
	</context-param>
		
	<!-- spring字符编码过滤器start-->
	<filter>
		<!--① Spring 编码过滤器 -->
	    <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>
    <!-- ② 过滤器的匹配 URL -->
    <filter-mapping> 
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	<!-- spring字符编码过滤器end-->
	
	
	<!-- Spring MVC配置 -->
	<!-- ====================================== -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	  
	
	
	<!-- Spring配置 -->
	<!-- ====================================== -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- log4j配置start -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<!-- Spring 加载 Log4j 的监听 -->
	<listener>
		<listener-class>
			org.springframework.web.util.Log4jConfigListener
		</listener-class>
	</listener>
	<!-- log4j配置end -->
	
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

applicationContext-mybtis.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:aop="http://www.springframework.org/schema/aop"  
            xmlns:p="http://www.springframework.org/schema/p"  
            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-2.5.xsd   
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
           
        <!-- Properties文件读取配置,base的properties -->  
        <context:property-placeholder location="classpath:jdbc.properties"/>  
           
        <!-- JNDI获取数据源(使用dbcp连接池) -->  
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
	    <property name="driverClassName" value="${driverClassName}"/>
	    <property name="url" value="${url}?useUnicode=true&characterEncoding=utf8"/>
	    <property name="username" value="${uname}"/>
	    <property name="password" value="${password}"/>
	 	</bean> 

        <!-- enable transaction demarcation with annotations -->  
        <tx:annotation-driven /> 
           
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource" ref="dataSource" />  
        </bean> 
         
        <!-- define the SqlSessionFactory, notice that configLocation is not needed when you use MapperFactoryBean -->  
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
            <property name="dataSource" ref="dataSource" />  
            <property name="configLocation" value="classpath:mybatis-config.xml" />  
        </bean> 
		<!-- scan for mappers and let them be autowired 
		MapperScannerConfigurer Mybatis-Spring 会自动为我们注册Mapper对应的MapperFactoryBean对象-->  
		<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
            <property name="basePackage" value="org.project.dao" />  
        </bean>
         
    
            
    </beans>  

 

jdbc.propertis

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/springmvcdb
uname=root
password=123

 

spring-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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	
	<!-- 使spring扫描包下的所有类,让标注spring注解的类生效 -->
	<context:component-scan base-package="org.project"/>
	
	<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	

</beans> 

 

mybatis-config.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">  
    <configuration>  
        <settings>  
            <!-- changes from the defaults -->  
            <setting name="lazyLoadingEnabled" value="false" />  
        </settings>  
       <typeAliases>  
           <!--这里给实体类取别名,方便在mapper配置文件中使用--> 
           <!-- <typeAlias alias="user" type="org.project.pojo.User"/>  -->
           <package name="org.project.pojo"/>
       </typeAliases>  
   </configuration>  

 

 

 

 

 

=====================


@Controller
public class IndexController {
    @Resource
    private UserService userService;
    
    
    @RequestMapping(value="/indexs",method=RequestMethod.GET)
    public String index(){
        
        return "index";
    }

--------------------------------------


@Service
public class UserServiceImpl implements UserService {
    
    @Resource
    private UserMapper userMapper;
   

 

springMVC+mybatis

标签:ati   方便   turn   odi   事务管理   val   tar   figure   url   

原文地址:http://www.cnblogs.com/m97i/p/7613781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!