spring-mvc-jpa環境搭建
Spring-mvc-JPA 是spring-mvc整合spring-data-jpa
具體配置如下:
(pom包含其他需要依賴)
pom.xml
<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.0</modelVersion> <groupId>com.third.spring-mvc-jpa</groupId> <artifactId>spring-mvc-jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-mvc-jpa</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.11.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.11.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.3.11.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.8.0.RELEASE</version> </dependency> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-provider</artifactId> <version>0.8.0-alpha1</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>ant</groupId> <artifactId>ant</artifactId> <version>1.6.5</version> </dependency> <dependency> <groupId>org.apache.avro</groupId> <artifactId>avro</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.3.0.Final</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.7.1.RELEASE</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
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" version="3.0"> <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/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--過濾器,防止亂碼 --> <filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </web-app>
springmvc-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--註解驅動配置--> <mvc:annotation-driven /> <!--自動掃描目錄下所有的類檔案 --> <context:component-scan base-package="com.java.controller" /> <!--對模型檢視名稱的解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-lazy-init="true">
<!-- 啟動元件掃描,排除@Controller元件,該元件由SpringMVC配置檔案掃描 -->
<context:component-scan base-package="com.java">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 屬性檔案位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 資料來源 -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<!-- 資料庫驅動 -->
<property name="driverClass" value="${jdbc.driver}" />
<!-- 相應驅動的jdbcUrl-->
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- 資料庫的使用者名稱 -->
<property name="username" value="${jdbc.username}" />
<!-- 資料庫的密碼 -->
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 設定JPA實現廠商的特定屬性 -->
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--方言 -->
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</bean>
<!-- JPA實體管理器工廠 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- 掃描包下的entity -->
<property name="packagesToScan" value="com.java" />
<property name="jpaProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- validate/update/create -->
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<!-- 建表的命名規則 -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
<!-- Jpa 事務配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.java" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<!-- 使用annotation定義事務 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db_spring_mvc_jpa?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
控制層編寫
package com.java.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.java.service.EmpService;
/*
* EMP 部門查詢是通過jpa解析方法名查詢
*
*/
@Controller
public class EmpController {
@Autowired
private EmpService empservice;
//查詢所有員工資訊t
@RequestMapping("getAllEmp")
public ModelAndView getAllEmp(){
ModelAndView model = new ModelAndView();
model.addObject("empList", empservice.getAllEmp());
model.setViewName("AllEmp");
return model;
}
//模糊查詢員工資訊
@RequestMapping(value = "getLikeEmp/{name}",method=RequestMethod.GET)
public ModelAndView getLikeEmp(@PathVariable String name) {
ModelAndView model = new ModelAndView();
model.addObject("listEmp", empservice.getLikeEmp(name));
model.setViewName("AllEmp");
return model;
}
}
服務層
package com.java.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.java.dao.EmpDao;
import com.java.model.Emp;
@Service
public class EmpService {
@Autowired
private EmpDao empdao;
//查詢所有員工資訊
public List<Emp> getAllEmp(){
return empdao.findAll();
}
//模糊查詢員工資訊
public List<Emp> getLikeEmp(String name){
return empdao.findByEnameLike(name);
}
}
模型層
package com.java.model;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "emp")
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int eid;
private String ename;
private Date hiredate;
private double salery;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public double getSalery() {
return salery;
}
public void setSalery(double salery) {
this.salery = salery;
}
}
資料層
package com.java.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.java.model.Emp;
@Repository
public interface EmpDao extends JpaRepository<Emp, Long>{
//模糊查詢員工資訊
public List<Emp> findByEnameLike(String name);
}
檢視層
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px" cellpadding="0px" cellspacing="0px">
<tr>
<td>編號</td>
<td>姓名</td>
<td>僱傭日期</td>
<td>薪水</td>
</tr>
<c:forEach var="h" items="${empList}">
<tr>
<td>${h.eid}</td>
<td>${h.ename}</td>
<td>${h.hiredate}</td>
<td>${h.salery}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
相關推薦
spring-mvc-jpa環境搭建
Spring-mvc-JPA 是spring-mvc整合spring-data-jpa 具體配置如下: (pom包含其他需要依賴) pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs
基於maven來Spring MVC的環境搭建遇到“坑”
nbsp 操作 應該 sdn 8.0 環境 art href 環境搭建 1、註解配置路徑問題; 在web.xml中配置spring mvc 路徑時, 應該配置如下:classpath:classpath:spring-* 2、jdk版本和Spring MVC版本不一致問
Spring MVC + TomCat 環境搭建
1.spring 需要的jar 包 spring框架配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSch
搭建Spring data jpa spring mvc hibernate5環境
“No persistence unit with name 'demo2' found 自己明明就在META-INF資料夾下建立了persistence .xml檔案 可以執行時就是得
Maven工程搭建spring boot+spring mvc+JPA
sep validate 賬戶 onu target implement handler meta 驗證 添加Spring boot支持,引入相關包: 1、maven工程,少不了pom.xml,spring boot的引入可參考官網: <parent>
Spring boot集成spring-boot-starter-data-jpa環境搭建
lis ESS hiberna 構建 TP xtend 默認 location col 1、創建Spring boot項目 2、保存等待構建完成 3、增加spring-boot-starter-data-jpa、內存數據庫依賴包hsqldb
idea 搭建maven+spring mvc+jpa框架
在此之前,對於搭建這樣一個框架很是苦惱,總是出錯 ,今天,作為一個初學者,將自己實驗多次的成功的步驟復原,希望能幫到需要的人 先說一下步驟流程(我認為比較快的步驟) 1、建立maven工程,並配置tomcat(看下一篇部落格) 2、建立java資料夾 3、配置pom.xml
搭建spring mvc開發環境
最近在網上看了很多關於spring mvc環境的搭建的文章,感覺不是很清晰,主要是程式碼不全,所以準備自己寫一個簡單的教程。 本次spring mvc環境的搭建使用的版本是Intellij IDEA 15.0.2+maven+Tomcat 8.5.
SpringData系列一Spring Data的環境搭建
vendor ssi over str spa ger repos getbean date 本節作為主要講解Spring Data的環境搭建 JPA Spring Data :致力於減少數據訪問層(DAO)的開發量。開發者唯一要做的就是聲音持久層的接口,其他都交給
spring boot開發環境搭建
oot osi jdbc 宋體 tid mes true err table 軟件151 朱實友 1.新建一個maven項目 Maven配置文件: <!-- Inherit defaults from Spr
Spring Boot的環境搭建
log main函數 ger warn 3.1 集合 font boot col 軟件152 陳卓 一、概念: 從最根本上來講,Spring Boot就是一些庫的集合,它能夠被任意項目的構建系統所使用。簡便起見,該框架也提供了命令行界面,它可以用來運行和測試Boot
Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(二)
自己 var user inf 接口 work 過程 cal ber 基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模塊項目(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模塊下創建com.bu
Spring Data 開發環境搭建(二)
是不是 lns utf-8 void ext for 實體類 connect domain 首先咱們先創建一個maven工程 在pom.xml加入以下 依賴 <!--Mysql 驅動包--> <dependency> <
IDEA 通過Maven創建Spring MVC項目搭建
javax cor 內容 imp ram als report spa 使用 概述 本篇隨筆主要記錄內容如下: 1、通過Maven創建基於Spring Framework類庫的MVC項目,免去了繁瑣的XML配置; 2、在Idea裏面配置To
spring+springmvc+mybatis環境搭建報錯問題(二)
1.啟動程式報錯: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
Spring+Spring MVC+Mybatis+Maven搭建多模組專案
maven多模組專案SpringMVC簡單例項:劃分多模組,也就是方便多人開發,自己開發自己的那塊沒有多大沖突。 專案結構 整個專案目錄是這樣的: GitHub地址 :https://github.com/thecattle/maven_model —-
Spring1:Spring簡介、環境搭建、原始碼下載及匯入MyEclipse
框架學習前言 這個模組是面向Spring的,Spring的學習我是這麼想的: 1、簡單介紹Spring,主要是從網上借鑑一些重點 2、儘量說明清楚Spring的使用方法以及細節點 3、儘量以自己的理解講清楚Spring中的一些原始碼 Spring是什麼 Spr
Spring+Spring MVC+Mybatis+Maven搭建多模組專案(二)
基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模組下建立com.bug.model.user包,在包中建立UserVO物件
Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)
最近在研究Spring MVC和Maven,工作中也是使用Spring MVC、Mybatis及Maven整合,出於好奇,自己也搭建了一個Spring+Spring MVC+Mybatis+Maven的多模組框架,先介紹一下我的工程結構 bug.root:根模
【Spring原始碼閱讀】Spring原始碼閱讀環境搭建
閱讀Spring原始碼,我們可以通過以下兩種方式: 直接在Idea/Eclipse上建立自己的java maven/gradle專案,匯入Spring特定版本依賴Jar和原始碼,直接執行測試程式碼,逐步除錯閱讀 從github直接獲取Spring原始碼專案,基於Spri