依赖库:
gradle + spring4 + springmvc4 + mybatis3 + driud1 + logback1
目录结构:
build.gradle
apply plugin: 'java' apply plugin: 'war' apply plugin: 'eclipse-wtp' apply plugin: 'jetty' // JDK 6 sourceCompatibility = 1.6 targetCompatibility = 1.6 repositories { mavenLocal() mavenCentral() } dependencies { compile 'ch.qos.logback:logback-classic:1.1.3' compile 'org.springframework:spring-webmvc:4.1.6.RELEASE' compile 'org.springframework:spring-tx:4.1.6.RELEASE' compile 'org.springframework:spring-jdbc:4.1.6.RELEASE' compile 'org.springframework:spring-test:4.1.6.RELEASE' compile 'javax.servlet:jstl:1.2' compile 'com.alibaba:druid:1.0.2' compile 'mysql:mysql-connector-java:5.1.25' compile 'org.mybatis:mybatis:3.2.2' compile 'org.mybatis:mybatis-spring:1.2.0' compile 'org.aspectj:aspectjweaver:1.7.2' compile 'org.freemarker:freemarker-gae:2.3.23' } // Embeded Jetty for testing jettyRun{ contextPath = "spring4" httpPort = 8080 } jettyRunWar{ contextPath = "spring4" httpPort = 8080 } //For Eclipse IDE only eclipse { wtp { component { //define context path, default to project folder name contextPath = 'spring4' } } }
spring-core-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 包扫描 --> <context:component-scan base-package="com.business.dao,com.business.service" /> <!-- 1.加载数据库配置的属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.数据源dataSource DRUID --> <!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <!-- <property name="validationQuery" value="${validationQuery}" /> --> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- 3.SessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 整合mybatis,包扫描 mapper文件 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <property name="mapperLocations" value="classpath:com/business/mapper/*.xml"/> </bean> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.taurus.persistence" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> --> <!-- 4. 事务 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="view*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.business.service.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
spring-mvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 "> <context:component-scan base-package="com.business.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:annotation-driven /> </beans>
web.xml
<web-app 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" version="2.5"> <display-name>BusinessOnline</display-name> <description>Spring MVC web application</description> <!-- For web context --> <servlet> <servlet-name>springmvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc-dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- For root context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-core-config.xml</param-value> </context-param> <!-- 编码过滤器,解决中文乱码 --> <filter> <filter-name>SpringEncoding</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> </filter> <filter-mapping> <filter-name>SpringEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="org.springframework" level="debug" additivity="false"> <appender-ref ref="STDOUT" /> </logger> <logger name="com.business" level="debug" additivity="false"> <appender-ref ref="STDOUT" /> </logger> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/student?characterEncoding=utf-8 jdbc.username=root jdbc.password=root #jdbc.driverClassName=oracle.jdbc.driver.OracleDriver #jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl #jdbc.username=scott #jdbc.password=tiger
sqlMapConfig.xm
<?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> </configuration>
jar:
实体类:
package com.business.domain; public class Student { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
dao
BaseDao:
package com.business.dao; import java.io.Serializable; import java.util.List; import java.util.Map; /** * 泛型类 基础的dao接口 * @author xiaoxuan * * @param <T> */ public interface BaseDao<T> { // public List<T> findPage(Page page); //分页查询 public List<T> find(Map paraMap); //带条件查询,条件可以为null,既没有条件;返回list对象集合 public T get(Serializable id); //只查询一个,常用于修改 public void insert(T entity); //插入,用实体作为参数 public void update(T entity); //修改,用实体作为参数 public void deleteById(Serializable id); //按id删除,删除一条;支持整数型和字符串类型ID public void delete(Serializable[] ids); //批量删除;支持整数型和字符串类型ID }
StudentDao:
package com.business.dao; import com.business.domain.Student; public interface StudentDao extends BaseDao<Student> { }
dao实现