springdata-jpa demo
程序员文章站
2022-04-01 14:37:04
...
demo 下载:
http://download.csdn.net/download/knight_black_bob/9421829
jar
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_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>/back/jsp/main.jsp</welcome-file> </welcome-file-list> <!-- 配置启动 IOC 容器的 Listener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置可以把 POST 请求转为 PUT、DELETE 请求的 Filter --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 OpenEntityManagerInViewFilter. 可以解决懒加载异常的问题 --> <filter> <filter-name>OpenEntityManagerInViewFilter</filter-name> <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置 SpringMVC 的 DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
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: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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.curiousby"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!-- 配置数据源 --> <context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <!-- 配置其他属性 --> </bean> <!-- 配置 JPA 的 EntityManagerFactory --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <property name="packagesToScan" value="com.curiousby"></property> <property name="jpaProperties"> <props> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="hibernate.cache.use_query_cache">true</prop> </props> </property> <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property> </bean> <!-- 配置事务 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <!-- 配置支持基于注解的事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置 SpringData --> <jpa:repositories base-package="com.curiousby" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories> </beans>
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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 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-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.curiousby" 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> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value=""></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> </beans>
package com.curiousby.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name="T_USER") @Entity public class User { private int userid; private String username; private String password; private String tel; private String sex; private String description; @Id @GeneratedValue(strategy = GenerationType.AUTO) public int getUserid() { return userid; } public String getUsername() { return username; } public String getPassword() { return password; } public String getTel() { return tel; } public String getSex() { return sex; } public String getDescription() { return description; } public void setUserid(int userid) { this.userid = userid; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTel(String tel) { this.tel = tel; } public void setSex(String sex) { this.sex = sex; } public void setDescription(String description) { this.description = description; } }
package com.curiousby.repository; import java.util.List; import javax.persistence.QueryHint; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.QueryHints; import com.curiousby.entity.User; /** * @author baoyou E-mail:curiousby@163.com * @version 2016年1月28日 下午1:13:09 * * desc: ... */ public interface UserRepository extends JpaRepository<User, Integer>{ @QueryHints({@QueryHint(name=org.hibernate.ejb.QueryHints.HINT_CACHEABLE,value="true")}) @Query("FROM User u") List<User> getAll(); }
package com.curiousby.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.curiousby.entity.User; import com.curiousby.repository.UserRepository; @Service public class UserService { @Autowired private UserRepository userRepository; @Transactional(readOnly = true) public List<User> allUser() { return userRepository.getAll(); } @Transactional(readOnly = true) public void delUser(int userId) { userRepository.delete(userId); } @Transactional(readOnly = true) public User user(int userId) { return userRepository.findOne(userId); } @Transactional(readOnly = true) public void updateUser(User user) { userRepository.saveAndFlush(user); } @Transactional public void addUser(User user) { userRepository.save(user); } }
package com.curiousby.action; import java.util.List; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.curiousby.entity.User; import com.curiousby.service.UserService; @Controller public class UserController { @Autowired UserService service; @RequestMapping(value = "/userView.do") public String userView(ModelMap modelMap,String pageNo, String choice, HttpSession session){ List<User> userList = service.allUser(); modelMap.put("userList", userList); return "back/jsp/user/userView"; } @RequestMapping(value = "/userDel{userId}.do") public String userDel(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){ service.delUser(userId); String message1="删除成功"; String message2="请返回……"; String url="userView.do"; modelMap.put("message1", message1); modelMap.put("message2", message2); modelMap.put("url", url); return "infomationShow"; } @RequestMapping(value ="/userGoAdd.do") public String userGoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){ return "back/jsp/user/userAdd"; } @RequestMapping(value = "/userAdd.do",method=RequestMethod.POST) public String userAdd(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){ service.addUser(form); String message1="添加成功"; String message2="请返回……"; String url="userView.do"; modelMap.put("message1", message1); modelMap.put("message2", message2); modelMap.put("url", url); return "infomationShow"; } @RequestMapping(value = "/userGoUpdate{userId}.do") public String userGoUpdate(@PathVariable("userId")int userId ,ModelMap modelMap,String pageNo, String choice, HttpSession session){ User user = service.user(userId); modelMap.put("user", user); return "back/jsp/user/userUpdate"; } @RequestMapping(value = "/userUpdate.do",method=RequestMethod.POST) public String userUpdate(User form,ModelMap modelMap,String pageNo, String choice, HttpSession session){ service.updateUser(form); String message1="修改成功"; String message2="请返回……"; String url="userView.do"; modelMap.put("message1", message1); modelMap.put("message2", message2); modelMap.put("url", url); return "infomationShow"; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
上一篇: nutz demo
推荐阅读
-
Android实现调用系统相册和拍照的Demo示例
-
jQuery插件FusionCharts实现的3D帕累托图效果示例【附demo源码】
-
jQuery插件FusionCharts实现的2D面积图效果示例【附demo源码下载】
-
oracle自定义函数demo分享
-
jQuery插件HighCharts实现的2D面积图效果示例【附demo源码下载】
-
jQuery插件HighCharts实现的2D条状图效果示例【附demo源码下载】
-
Mac下部署springBoot项目到Docker中(demo)
-
微信小程序使用websocket通讯的demo,含前后端代码,亲测可用
-
Dev treelist两张表父子节点设置demo
-
ASP.net Forms验证Demo第1/3页