亲测可用,2014-12-4
- - - - - - - - - - - - - - - - - - - - - - - 项目所需架包 libs- - - - - - - - - - - - - - - - - - - - - -
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjweaver-1.5.3.jar
c3p0-0.9.1.jar (连接池)
commons-fileupload-1.2.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
jstl.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.18-bin.jar (Mysql数据库连接lib)
oscache-2.4.1.jar (缓存lib,根据个人需求)
slf4j-api-1.6.1.jar
spring-aop-3.2.4.RELEASE.jar
spring-aspects-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar
standard.jar
- - - - - - - - - - - - - - - - - - - - - - - - 相关配置文件- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -- - - - - - - - - -- - - - - - - - - -web.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配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring MVC配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/servlet-context.xml</param-value> <!--指定XML文件位置-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<!-- 设置字符集 -->
<span style="white-space:pre"> </span> <filter>
<span style="white-space:pre"> </span> <filter-name>encodingFilter</filter-name>
<span style="white-space:pre"> </span> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<span style="white-space:pre"> </span> <init-param>
<span style="white-space:pre"> </span> <param-name>encoding</param-name>
<span style="white-space:pre"> </span> <param-value>UTF-8</param-value>
<span style="white-space:pre"> </span> </init-param>
<span style="white-space:pre"> </span> <init-param>
<span style="white-space:pre"> </span> <param-name>forceEncoding</param-name>
<span style="white-space:pre"> </span> <param-value>true</param-value>
<span style="white-space:pre"> </span> </init-param>
<span style="white-space:pre"> </span> </filter>
<span style="white-space:pre"> </span> <filter-mapping>
<span style="white-space:pre"> </span> <filter-name>encodingFilter</filter-name>
<span style="white-space:pre"> </span> <url-pattern>/*</url-pattern>
<span style="white-space:pre"> </span> </filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- - - - - - -- - - - - - - - - -db.properties 关于数据库的配置文件- - - - - - - - - -- - - - - - - - -
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test
db.username=root
db.password=root
- - - - - - - - -- - - - - - - - - -servlet-context.xml : springMvc 配置- - - - - - - - - -- - - - - - - - - -- - - - -
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/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">
<!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 注解扫描的包 -->
<context:component-scan base-package="com.test.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<span style="white-space:pre"> </span><!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 annotation默认的办法映射适配器 -->
<span style="white-space:pre"> </span><bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<span style="white-space:pre"> </span><bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<span style="white-space:pre"> </span><!-- 上传文件bean -->
<span style="white-space:pre"> </span><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" />
<span style="white-space:pre"> </span>
</beans>
- - - - - - - - -- - - - - - - - - -spring-context.xml : spring 整合 hibernate 配置- - - - - - - - - -- - - - - - - - - -- - - - -
<?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: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/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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<span style="white-space:pre"> </span><!-- 自动加载构建bean -->
<context:component-scan base-package="com.test" />
<!-- 加载数据库属性配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池c3p0配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="jdbcUrl" value="${db.url}"></property>
<property name="driverClass" value="${db.driverClassName}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<span style="white-space:pre"> </span> <property name="dataSource" ref="dataSource" />
<span style="white-space:pre"> </span></bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<span style="white-space:pre"> </span> <property name="configLocation" value="classpath:mybatis-config.xml" />
<span style="white-space:pre"> </span> <property name="dataSource" ref="dataSource" />
<span style="white-space:pre"> </span> <!-- mapper和resultmap配置路径 -->
<span style="white-space:pre"> </span> <property name="mapperLocations">
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> <list>
<value>classpath:com/test/entity/*-resultmap.xml</value>
<span style="white-space:pre"> </span> <value>classpath:com/test/mapper/*-mapper.xml</value>
<span style="white-space:pre"> </span> </list>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> </property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 通过扫描的模式,扫描目录在com/test/mapper目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了 -->
<span style="white-space:pre"> </span> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<span style="white-space:pre"> </span> <property name="basePackage" value="com.test.mapper"/>
<span style="white-space:pre"> </span> <property name="markerInterface" value="com.test.mapper.SqlMapper"/>
<span style="white-space:pre"> </span> </bean>
<span style="white-space:pre"> </span>
</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>
<typeAliases>
<typeAlias alias="user" type="com.test.entity.User"/>
</typeAliases>
</configuration>
- - - - - - - - - - - - -User Entity - - - - - - - - - - - - - - - -
package com.test.entity;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 6334108012097409172L;
private int id;
private String userName;
private String password;
private String email;
private int age;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return this.id + "#" + this.userName + "#" + this.password + "#" + this.email + "#" + this.age;
}
}
- - - - - - - - - - - - - - - -user-resultmap.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">
<mapper namespace="userMap">
<resultMap type="com.test.entity.User" id="userResultMap">
<id property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="age" column="age"/>
</resultMap>
</mapper>
- - - - - - - - - - - - - - -SqlMapper .java ( 所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了,详情参照spring-context.xml 最下面)- - - - - - - - - - - - - - - - - -
package com.test.mapper;
public interface SqlMapper {
}
- - - - - - - - - - - - - - - - -UserMapper .java- - - - - - - - - - - - - - - - - -
package com.test.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.test.entity.User;
@Repository
@Transactional
public interface UserMapper extends SqlMapper {
public List<User> list();
public User getUser(int id);
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(int UserId);
}
- - - - - - - - - - - - - - - - -user-mapper.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">
<mapper namespace="com.test.mapper.UserMapper">
<select id="list" resultType="list" resultMap="userMap.userResultMap">
SELECT * FROM t_user
</select>
<!-- id和mapper接口中的方法名对应,resultType使用mybatis-config.xml中的别名 -->
<select id="getUser" parameterType="int" resultType="user">
SELECT * FROM t_user WHERE id=#{id}
</select>
<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="user">
INSERT INTO t_user (id,userName,password,email,age)
VALUES (#{id},#{userName},#{password},#{email},#{age})
</insert>
<update id="updateUser" parameterType="user">
UPDATE t_user SET userName=#{userName},password=#{password},email=#{email},age=#{age} WHERE id=#{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM t_user WHERE id=#{id}
</delete>
</mapper>