Maven学习笔记(十)-Maven整合SSHSJ(Spring+SpringMVC+Hibernate+Spring Data JPA+JdbcTemplate)框架

本文主要是介绍Maven学习笔记(十)-Maven整合SSHSJ(Spring+SpringMVC+Hibernate+Spring Data JPA+JdbcTemplate)框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、所用版本与步骤


四个框架所使用的版本:

Spring ---- 4.1.3.RELEASE

Spring mvc ---- 4.1.3.RELEASE

Spring Data JPA----1.3.2.RELEASE

hibernate ---- 4.3.11.Final


其它:

junit ---- 4.12
MySQL ---- 5.1.35
log4j ---- 1.2.17
c3p0 ---- 0.9.2.1

jstl ---- 1.2


二、整合框架


步骤和前面的Spring+SpringMVC+Hibernate整合差不多,主要的差别就是这里使用了Spring Data JAP中的EntityManagerFactory,而不是Hibernate中的SessionFactory


JPA使用EntityManagerFactory开闭session,而Hibernate使用SessionFactory开闭session。两者区别:

1. EntityManagerFactory是JPA的标准API,如果使用EntityManagerFactory,在更换实现JPA的ORM框架时,需要改动的代码很少。如果使用SessionFactory,因为SessionFactory是Hibernate的,无法直接更换ORM框架,更换ORM的话,需要修改所有的代码。

2. 如果想使用Hibernate的某些高级特性,只能使用SessionFactory,使用EntityManagerFactory无法享受Hibernate的高级用法。


项目结构:





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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.fendo</groupId><artifactId>JPA</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>JPA Maven Webapp</name><url>http://maven.apache.org</url><properties><junit.version>4.12</junit.version><servlet.version>3.1.0</servlet.version><jstl.version>1.2</jstl.version><taglibs.version>1.1.2</taglibs.version><spring.version>4.1.3.RELEASE</spring.version><jpa.version>1.3.2.RELEASE</jpa.version><hibernat.version>4.3.11.Final</hibernat.version><mysql.version>5.1.35</mysql.version><dhcp.version>1.2.2</dhcp.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${servlet.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>${taglibs.version}</version></dependency><!-- Spring start --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>4.1.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-jpa</artifactId><version>${jpa.version}</version></dependency><!-- Hibernate --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>${hibernat.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>${hibernat.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-ehcache</artifactId><version>${hibernat.version}</version></dependency><!-- Mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>${dhcp.version}</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>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-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>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></dependencies><build><plugins><!-- 编码和编译 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>utf8</encoding></configuration></plugin></plugins><finalName>JPA</finalName></build>
</project>



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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsdhttp://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.fendo"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><!-- 属性文件位置 --><context:property-placeholder location="classpath:config.properties" /><!-- 数据源 --><bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"destroy-method="close"><!-- 数据库驱动 --><property name="driverClass" value="${jdbc.driverClassName}" /><!-- 相应驱动的jdbcUrl--><property name="jdbcUrl" value="${jdbc.url}" /><!-- 数据库的用户名 --><property name="username" value="${jdbc.username}" /><!-- 数据库的密码 --><property name="password" value="${jdbc.password}" /></bean><!-- JPA实体管理器工厂 --><bean id="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource" /><property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /><!-- 加入定制化包路径 --><property name="packagesToScan" value="com.fendo.entity" /><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="hibernateJpaVendorAdapter"class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><property name="databasePlatform" value="${hibernate.dialect}"/></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.fendo"  transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/><!-- 使用annotation定义事务 --><tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>


Spring-mvc.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" ><!-- 启用SpringMVC的注解功能,它会自动注册HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例 --><mvc:annotation-driven /><!-- SpringMVC的扫描范围 --><context:component-scan base-package="com.fendo.controller" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><!-- 默认访问跳转到登录页面,即定义无Controller的path<->view直接映射 <mvc:view-controller path="/" view-name="redirect:/login"/>--><!-- 静态文件访问 --><!-- <mvc:resources mapping="/resources/**" location="/resources/" /> --><!-- 用于返回json格式 --><bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">    <property name="supportedMediaTypes">    <list><value>application/x-www-form-urlencoded;charset=UTF-8</value></list></property></bean><!-- 完成请求和注解POJO的映射 --><bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /></list></property></bean><!-- 配置SpringMVC的视图解析器 --> <!-- 其viewClass属性的默认值就是org.springframework.web.servlet.view.JstlView -->  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> --><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean><!-- 静态资源加载 --><mvc:resources location="/css/" mapping="/css/**" /><mvc:resources location="/images/" mapping="/images/**" /><mvc:resources location="/ATTACHMENT/" mapping="/ATTACHMENT/**" /></beans>

config.properties文件为:

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8jdbc.username=rootjdbc.password=123hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialectBoneCP.idleConnectionTestPeriod=60BoneCP.idleMaxAge=60BoneCP.maxConnectionsPerPartition=5BoneCP.minConnectionsPerPartition=1BoneCP.partitionCount=3BoneCP.acquireIncrement=2  BoneCP.statementsCacheSize=0 BoneCP.releaseHelperThreads=3


web.xml文件为:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"><display-name>Archetype Created Web Application</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext*.xml</param-value></context-param><!-- 防止发生java.beans.Introspector内存泄露,应将它配置在ContextLoaderListener的前面 -->  <!-- 详细描述见http://blog.csdn.net/jadyer/article/details/11991457 -->  <listener>  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  </listener>  <!-- 实例化Spring容器 -->  <!-- 应用启动时,该监听器被执行,它会读取Spring相关配置文件,其默认会到WEB-INF中查找applicationContext.xml -->  <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置编码过滤器 --><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><!-- 配置spring管理OpenEntityManagerInViewFilter--><!-- OpenEntityManagerInViewFilter会让session一直到view层调用结束后才关闭Spring针对Hibernate的非JPA实现用的是OpenSessionInViewFilter,原理与这个大同小异--><filter><filter-name>hibernateFilter</filter-name><filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class></filter><filter-mapping><filter-name>hibernateFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 用默认的servlet来处理资源 --><servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern>    </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern>    </servlet-mapping>     <servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern>    </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.woff</url-pattern>    </servlet-mapping> <servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.ttf</url-pattern>    </servlet-mapping>          <servlet-mapping>  <servlet-name>default</servlet-name> <url-pattern>*.map</url-pattern>    </servlet-mapping>      <servlet-mapping>      <servlet-name>default</servlet-name>   <url-pattern>*.js</url-pattern>   </servlet-mapping>  <servlet-mapping>       <servlet-name>default</servlet-name>      <url-pattern>*.css</url-pattern>     </servlet-mapping>  <servlet-mapping>       <servlet-name>default</servlet-name>      <url-pattern>*.html</url-pattern>     </servlet-mapping><!-- SpringMVC核心分发器 --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>/index.html</welcome-file></welcome-file-list>
</web-app>


User实体类:

package com.fendo.entity;import java.io.Serializable;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "tb_user")
public class User implements Serializable {private static final long serialVersionUID = 7419229779731522702L;@Id@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id")private Long id;@Column(name="user_name")private String name;@Column(length = 50)private String account;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public static long getSerialversionuid() {return serialVersionUID;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

UserDao类:


package com.fendo.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.fendo.entity.User;public interface UserDao extends JpaRepository<User, Long> {}

UserService类:

package com.fendo.service;import java.util.List;import javax.transaction.Transactional;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;import com.fendo.dao.UserDao;
import com.fendo.entity.User;@Service
@Transactional
public class UserService {@Autowiredprivate UserDao userDao;public String add(User user){userDao.save(user);return "添加成功!";}public String addUser(String account){User user = new User();user.setAccount(account);userDao.save(user);return "添加成功!";}public User getOneUser(Long id){return userDao.findOne(id);}public List<User> findAll(){return userDao.findAll();}}


UserController类:
package com.fendo.controller;import java.util.List;import javax.persistence.Column;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import com.fendo.entity.User;
import com.fendo.service.UserService;@Controller
@RequestMapping(value="user")
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/addUser")    public ModelAndView addPerson(User user){    System.out.println("页面数据:"+user.toString());    //加入数据    userService.add(user);    //查数据    List<User> ls=userService.findAll();//存起来    ModelAndView modelAndView=new ModelAndView();    modelAndView.setViewName("success");    modelAndView.addObject("user", ls);  return modelAndView;    } }

index.jsp


<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>    
<!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>注册页面</title>    
</head>    
<body>    <form action="user/addUser" method="post">    name:<input type="text" name="name"/><br/>    <input type="submit" value="添加"/><br/>    </form>    
</body>    
</html>    

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>        
<!-- EL表达式生效 -->    
<%@ page isELIgnored="false" %>    
<!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="1">    <tr>    <th>姓名</th>    </tr>   <c:forEach items="${user}" var="p">    <tr>    <td>${p.name}</td>    </tr>    </c:forEach>  </table>    
</body>    
</html>    



加入JdbcTemplate


然后在applicationContext.xml中注入JdbcTemplate


    <!-- 实例化jdbcTemplate,同时注入数据源 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"p:dataSource-ref="dataSource" />

新建JdbcDao类:


package com.fendo.dao;import java.util.List;import com.fendo.entity.User;public interface JdbcDao {public String searchUserName(int id);public List<User> findAll();
}


JdbcDaoImp实现类:


package com.fendo.dao.imp;import java.util.List;
import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;import com.fendo.dao.JdbcDao;
import com.fendo.entity.User;@Service
public class JdbcDaoImp implements JdbcDao{@AutowiredJdbcTemplate jdbcTemplate;@Overridepublic String searchUserName(int id) {String sql = "select user_name from tb_user where id=?";  // 返回类型为String(String.class)  return jdbcTemplate.queryForObject(sql, String.class, id);  }@Overridepublic List<User> findAll() {String sql="select * from tb_user";List<Map<String,Object>> lists = jdbcTemplate.queryForList(sql); return User.toObject(lists);  }}


在User实体类中加入:


public static User toObject(Map map) {  User user = new User();  user.setId((Long) map.get("id"));  user.setName((String) map.get("user_name"));  return user;  }  public static List toObject(List<Map<String,Object>> lists){  List users = new ArrayList();  for (Map map : lists) {  User user =  User.toObject(map);  if (user != null) {  users.add(user);  }  }  return users;  }  



然后在UserController注入:


    @Autowiredprivate JdbcDaoImp jdbcDaoImp;


写个获取所有数据的Controller


@RequestMapping("/getAll")public ModelAndView getAll(){List<User> lst=jdbcDaoImp.findAll();ModelAndView modelAndView=new ModelAndView();    modelAndView.setViewName("success");    modelAndView.addObject("user", lst);  return modelAndView;}



三、测试功能


运行项目访问:http://localhost:8099/JPA/index.jsp


效果如下:



输入用户名点击添加,添加成功之后,打印所有的数据:




完整示例: http://download.csdn.net/detail/u011781521/9866948 没有加入JdbcTemplate


通过JdbcTemplate获取所有的数据: http://localhost:8099/JPA/user/getAll





带JdbcTemplate的示例: http://download.csdn.net/detail/u011781521/9867172


这篇关于Maven学习笔记(十)-Maven整合SSHSJ(Spring+SpringMVC+Hibernate+Spring Data JPA+JdbcTemplate)框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068625

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06