使用Maven实现SSS框架的整合。
方便记录,专门建了一个pom项目用来整合SSS框架所用的jar包
pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.qfgroupId>
<artifactId>M_SSS_PomartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<description>这是SSS整合框架的所有依赖description>
<dependencies>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-jpaartifactId>
<version>1.11.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-c3p0artifactId>
<version>5.2.10.Finalversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.39version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.2version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.6.0version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
plugins>
build>
project>
pom文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.qfgroupId>
<artifactId>M_SSS_PomartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<groupId>org.qfgroupId>
<artifactId>M_SSS_WarartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<description>这是整合的Spring和SpringMVC和Sprng Datadescription>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.36version>
dependency>
dependencies>
project>
接下来开始代码的编写:
@Entity
@Table(name="tb_student")
@NamedQuery(name="Student.querys",query="select s from Student s")
public class Student {
//@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")//日期格式化
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(length=50)
private String name;
@Column(length=2)
private String sex;
//@Basic
//@Transient只是属性不是字段
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Spring Data JPA最大的改变就是dao层,只需定义接口即可,记住要继承指定的接口
public interface StudentDao extends JpaRepository<Student, Serializable>{
Student findById(int id);
Student findByIdAndName(int id,String name);
}
public interface StudentService {
boolean save(Student stu);
List queryAll();
Student querySingle(int id);
}
@Service
public class StudentServiceImple implements StudentService{
@Autowired
private StudentDao dao;
@Override
public boolean save(Student stu) {
// TODO Auto-generated method stub
System.out.println("---->"+dao.getClass().getName());
//有主键就修改,没有主键就新增
//System.out.println(1/0);
return dao.save(stu)!=null?true:false;
}
@Override
public List queryAll() {
// TODO Auto-generated method stub
//System.out.println("数量:"+dao.count());
return dao.findAll();
}
@Override
public Student querySingle(int id) {
// TODO Auto-generated method stub
return dao.findById(id);
}
}
@Controller
public class StudentController {
@Autowired
private StudentService service;
@RequestMapping("/{jn}")
public String pre(@PathVariable String jn) {
return jn;
}
@RequestMapping("/save")
public void test1(Student stu) {
if (stu!=null &&stu.getName()!=null &&stu.getName().length()>0 ) {
stu.setId(2);
System.out.println("控制器:新增:" + service.save(stu));
}
}
@RequestMapping("/queryall")
public void test2(Model m) {
System.out.println("控制器:查询全部");
if (!m.containsAttribute("list")) {
m.addAttribute("list", service.queryAll());
}
}
@RequestMapping("/querysingle")
public String test3(int id, Model m) {
System.out.println("控制器:查询单个");
List list=new ArrayList<>();
list.add(service.querySingle(id));
m.addAttribute("list", list);
return "queryall";
}
}
新增数据页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增学生title>
head>
<body>
<form action="save">
姓名:<input name="name"/><br/>
性别:<input name="sex"/><br/>
年龄:<input name="age"/><br/>
<input type="submit" value="新增学生"/><br/>
form>
<a href="queryall">查询所有学生a>
body>
html>
查询数据页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>所有学生title>
head>
<body>
<a href="queryall">刷新数据a>
<a href="save">添加学生a>
<table width="70%" align="center" border="1">
<c:forEach items="${list }" var="s">
<tr>
<td>${s.id }td>
<td>${s.name }td>
<td>${s.sex }td>
<td>${s.age }td>
tr>
c:forEach>
table>
body>
html>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="dbconfig" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistenceprovider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sss_sys?characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="lx" />
<property name="hibernate.connection.password" value="lx" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect" />
<property name="hibernate.max_fetch_depth" value="10" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="javax.persistence.validation.mode" value="none"/>
properties>
persistence-unit>
persistence>
<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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:component-scan base-package="cn.code404" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
context:component-scan>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="dbconfig">property>
<property name="persistenceXmlLocation" value="classpath:persistence.xml">property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
bean>
property>
bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="cn.code404" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory" />
beans>
<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:beans="http://www.springframework.org/schema/beans"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.qf.web.controller" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<beans:constructor-arg value="UTF-8" />
beans:bean>
<beans:bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<beans:property name="charset" value="UTF-8" />
<beans:property name="supportedMediaTypes">
<beans:list>
<beans:value>application/jsonbeans:value>
<beans:value>text/html;charset=UTF-8beans:value>
beans:list>
beans:property>
<beans:property name="features">
<beans:list>
<beans:value>WriteMapNullValuebeans:value>
<beans:value>QuoteFieldNamesbeans:value>
<beans:value>WriteDateUseDateFormatbeans:value>
<beans:value>WriteEnumUsingToStringbeans:value>
beans:list>
beans:property>
beans:bean>
mvc:message-converters>
mvc:annotation-driven>
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/">property>
<property name="suffix" value=".jsp">property>
bean>
beans>
<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_2_5.xsd" version="2.5">
<display-name>M_SSS_Wardisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springMVC.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
ok,发布到Tomcat即可进行操作。