本文会使用IDEA工具来搭建Spring+SpirngMVC+SpringDataJPA企业常用框架
项目的下载地址是:http://download.csdn.net/download/hongtao_scau/9964916
Spring JPA通过为用户统一创建和销毁EntityManager,进行事务管理,简化JPA的配置等使用户的开发更加简便。Spring Data JPA是在Spring JPA的基础上,对持久层做了简化。用户只需声明持久层的接口,不需要实现该接口。Spring Data JPA内部会根据不同的策略、通过不同的方法创建Query操作数据库。
4.0.0
war
test
com.gosuncn
base
1.0-SNAPSHOT
test
org.apache.maven.plugins
maven-compiler-plugin
1.6
UTF-8
UTF-8
4.2.5.RELEASE
5.1.29
1.7.18
1.2.17
5.0.1.Final
1.0.1.Final
4.2.6.Final
jstl
jstl
1.2
junit
junit
4.11
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-test
${spring.version}
org.hibernate
hibernate-core
${version-hibernate}
org.hibernate
hibernate-entitymanager
${version-hibernate}
cglib
cglib
dom4j
dom4j
org.hibernate
hibernate-validator
${version-hibernate.validator}
org.hibernate.javax.persistence
hibernate-jpa-2.0-api
${version-hibernate.jpa}
org.hibernate
hibernate-ehcache
${version-hibernate}
org.springframework.data
spring-data-jpa
1.6.2.RELEASE
mysql
mysql-connector-java
${mysql-driver.version}
commons-dbcp
commons-dbcp
1.2.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
com.fasterxml.jackson.core
jackson-core
2.8.0
com.fasterxml.jackson.core
jackson-databind
2.8.0
com.thetransactioncompany
cors-filter
2.4
com.thetransactioncompany
java-property-utils
1.9.1
com.alibaba
druid
1.0.14
jdbc.driverClasss=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dna?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useOldAliasMetadataBehavior=true
jdbc.username=root
jdbc.password=root
#定义初始连接数
jdbc.initialSize=0
#定义最大连接数
jdbc.maxActive=20
#定义最大空闲
jdbc.maxIdle=20
#定义最小空闲
jdbc.minIdle=1
#定义最长等待时间
jdbc.maxWait=60000
log4j.rootLogger=INFO,Console,File
#控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
#普通文件日志
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
org.hibernate.cfg.ImprovedNamingStrategy
true
update
test
contextConfigLocation
classpath:applicationContext.xml
log4jConfigLocation
classpath:log4j.properties
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-web.xml
1
SpringMVC
/
20
/index.jsp
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50548
Source Host : localhost:3306
Source Database : dna
Target Server Type : MYSQL
Target Server Version : 50548
File Encoding : 65001
Date: 2017-09-05 11:50:57
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2017 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('2015', '小李', '456');
INSERT INTO `user` VALUES ('2016', '大牛', '123');
package base.bean;
import javax.persistence.*;
/**
* Created by Administrator on 2017/9/4 0004.
*/
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
}
package base.dao;
import base.bean.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 2017/9/4 0004.
*/
@Repository
public interface UserDAO extends JpaRepository{
}
package base.controller;
import base.bean.User;
import base.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
/**
* Created by Administrator on 2017/9/4 0004.
*/
@Controller
@RequestMapping("/test")
public class base {
@Autowired
UserDAO userDAO;
@RequestMapping("/index")
public ModelAndView test(){
ModelAndView modelAndView =new ModelAndView("index");
modelAndView.addObject(userDAO.findOne(2015));
return modelAndView;
}
@RequestMapping("/getdata")
@ResponseBody
public User getdata(){
return userDAO.findOne(2015);
}
}
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2017/9/4 0004
Time: 14:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
23${user.name}
corsFilter
base.filter.SimpleCORSFilter
corsFilter
/*
自己写的支持跨域的拦截器
package base.filter;
/**
* Created by Administrator on 2017/4/30 0030.
*/
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class SimpleCORSFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires,Content-Type, X-E4M-With");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
org.hibernate.cfg.ImprovedNamingStrategy
true
update