首先建立javaee开发的三层结构
导入配置文件 jdbc.properties log4j.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern= %d [%-5p] %c - %m%n
#show sql
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
导入spring 配置文件
applicationContext-dao.xml:配置数据源,加载mybatis配置文件 mybatis-config.xml,mapper接口映射
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location= "classpath:jdbc.properties"/>
<bean id ="dataSource" class=" org.apache.commons.dbcp.BasicDataSource">
<property name ="driverClassName" value= "${jdbc.driver}"/>
<property name ="url" value="${jdbc.url}"/>
<property name ="username" value= "${jdbc.username}"/>
<property name ="password" value= "${jdbc.password}"/>
<property name ="maxActive" value="10"/>
<property name ="maxIdle" value="5"/>
</bean >
<bean id ="sqlSessionFactory" class= "org.mybatis.spring.SqlSessionFactoryBean" >
<property name ="dataSource" ref= "dataSource"/>
<property name ="configLocation" value= "classpath:mybatis-config.xml"/>
<!-- <property name="typeAliasesPackage" value="com.hsj.bean"></property> -->
</bean >
<bean class= "org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name ="basePackage" value= "com.hsj.mapper"/>
</bean >
</beans>
applicationContext-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package= "com.hsj.service"/>
</beans>
applicationContext-trans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
" >
<bean id ="transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name ="dataSource" ref="dataSource"/>
</bean >
<tx:advice id ="txAdvice" transaction-manager="transactionManager" >
<tx:attributes >
<!-- 传播行为 -->
<tx:method name ="save*" propagation="REQUIRED" />
<tx:method name ="insert*" propagation="REQUIRED" />
<tx:method name ="delete*" propagation="REQUIRED" />
<tx:method name ="update*" propagation="REQUIRED" />
<tx:method name ="find*" propagation="SUPPORTS" read-only= "true" />
<tx:method name ="get*" propagation="SUPPORTS" read-only= "true" />
<tx:method name ="query*" propagation="SUPPORTS" read-only= "true" />
</tx:attributes >
</tx:advice >
<aop:config >
<aop:advisor advice-ref ="txAdvice" pointcut= "execution(* com.hsj.service.*.*(..))" />
</aop:config >
</beans>
springmvc.xml
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<context:component-scan base-package= "com.hsj.controller"/>
<mvc:annotation-driven />
<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name ="prefix" value= "/WEB-INF/jsp/"></property >
<property name ="suffix" value=".jsp"></ property>
</bean >
</beans>
配置 web.xml
<?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>springmvc- mybatis</ display-name>
< 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>
<!-- 配置spring -->
<context-param >
<param-name >contextConfigLocation </param-name >
<param-value >classpath:applicationContext*. xml</ param-value>
</ context-param>
<!-- 配置监听器加载Spring配置文件 -->
< listener>
<listener-class >org.springframework.web.context.ContextLoaderListener </listener-class >
</ listener>
< 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.xml </param-value >
</init-param >
</ servlet>
< servlet-mapping>
<servlet-name >springMvc </servlet-name >
<url-pattern >/ </url-pattern >
</ servlet-mapping>
</web-app>