SpringMVC+JPA使用注入的方式环境搭建
----------------------------------------------------DAO-----------------------------------------------------------
package com.jb.jubmis.Dao.DaoImpl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import sun.security.provider.MD5;
import com.jb.jubmis.Dao.PubUserDao;
import com.jb.jubmis.entity.Pubuser;
import com.jb.jubmis.entity.Xskh;
public class PubUserDaoImpl extends JpaDaoSupport implements PubUserDao {
@SuppressWarnings("unchecked")
public List<Pubuser> login(final Pubuser pubuser) {
try {
//List<Pubuser> user=super.getJpaTemplate().find(" select u from Pubuser u where u.acode='"+pubuser.getAcode()+"'");
List ret = (List) this.getJpaTemplate().execute(
new JpaCallback() {
public Object doInJpa(EntityManager em){
Query query = null;
query = em.createQuery(" select u from Pubuser u where u.password=:p and u.status=0 and (u.account=:a or u.code=:a) ");
query.setParameter("a", pubuser.getAcode());
query.setParameter("p",pubuser.getPass());
return query.getResultList();
}
});
return ret;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//获取最大序号
@SuppressWarnings("unchecked")
public Integer getMaxPxxh() {
Integer pxxh=0;
try {
List<Pubuser> xslist=super.getJpaTemplate().find(" select x from Pubuser x order by x.serialNum desc ");
if(xslist.size()>0){
pxxh=xslist.get(0).getPxxh();
}
} catch (Exception e) {
System.out.println("获取最大序号出错!"+e.getMessage());
}
return pxxh;
}
public Pubuser register(Pubuser pubuser) {
try {
super.getJpaTemplate().persist(pubuser);
return pubuser;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
public List<Pubuser> checkInfo(String sql) {
try {
return super.getJpaTemplate().find(sql);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
----------------------------------------------------services-----------------------------------------------------------
package com.jb.jubmis.Services.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.jb.jubmis.Dao.PubUserDao;
import com.jb.jubmis.Services.PubUserServices;
import com.jb.jubmis.comm.Comm;
import com.jb.jubmis.entity.Pubuser;
public class PubUserServicesImpl implements PubUserServices {
private PubUserDao pubUserDao;
// 登
public List<Pubuser> saveInfo(Pubuser user) {
List<Pubuser> list=new ArrayList<Pubuser>();
try {
// 登录
user.setPass(Comm.encode(user.getPass()));
list = pubUserDao.login(user);
return list;
} catch (Exception e) {
return list;
}
}
// 注册
@Transactional
public Pubuser saveRegister(Pubuser user) {
Pubuser pu = new Pubuser();
try {
// 注册
user.setAguid(Comm.getAguid());
user.setDateIn(Comm.getDate());
user.setPxxh(getMaxPxxh());
user.setPass(Comm.encode(user.getPass()));//MD5 PWD
pu = pubUserDao.register(user);
return user;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
//获取最大序号
public Integer getMaxPxxh(){
return this.pubUserDao.getMaxPxxh()+1;
}
//帐号是否存在
public List<Pubuser> checkInfo(Pubuser user) {
List<Pubuser> list=new ArrayList<Pubuser>();
try {
list = pubUserDao.checkInfo("select u from Pubuser u where u.code='"+user.getAcode()+"'");
return list;
} catch (Exception e) {
return list;
}
}
public void setPubUserDao(PubUserDao pubUserDao) {
this.pubUserDao = pubUserDao;
}
}
----------------------------------------------------action-----------------------------------------------------------
package com.jb.jubmis.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.google.gson.reflect.TypeToken;
import com.jb.jubmis.Services.ExcelServices;
import com.jb.jubmis.Services.PubUserServices;
import com.jb.jubmis.comm.Comm;
import com.jb.jubmis.entity.Pubuser;
import com.jb.jubmis.entity.Xskh;
public class PubuserAction extends MultiActionController {
private PubUserServices pubUserServices;
private ExcelServices excelServices;
private String indexPage;
private String loginPage;
private String registerPage;
private Integer pageNo=1;
private Integer pageSize=15;
private Pubuser user =new Pubuser();
public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
Map map=new HashMap();
List<Xskh> xskh=new ArrayList<Xskh>();
try {
String acode=request.getParameter("acode");
String pass=request.getParameter("pass");
user.setAcode(acode);
user.setPass(pass);
List<Pubuser> us=pubUserServices.saveInfo(user);//登录
if(us.size()>0){
Map<Object, Object> mapinf=new HashMap<Object, Object>();
mapinf.put("aguid", us.get(0).getAguid());
mapinf.put("pageNo", -1);//查询所有
mapinf.put("pageSize",pageSize);
//计算分页
int size=excelServices.getInfo(mapinf).size();
request.setAttribute("size",size);
if(size==0){
size=1;
}
int pageAll=size/pageSize;//计算总页数
if(size%pageSize!=0){
pageAll++;
}
//分页查询
mapinf.remove("pageNo");
mapinf.put("pageNo", pageNo);
mapinf.put("pageSize",pageSize);
xskh=excelServices.getAll(mapinf);
request.setAttribute("pageNo",pageNo);//当前页
request.setAttribute("pageAll",pageAll);//末页
map.put("xskh", xskh);
request.getSession().setAttribute("user",us.get(0));
return new ModelAndView(getIndexPage(),map);
}else{
request.setAttribute("msg", "抱歉,帐号或密码不对!");
return new ModelAndView(getLoginPage(),map);
}
} catch (Exception e) {
//e.printStackTrace();
System.err.println("登录或注册:"+e.getMessage());
//失败
request.setAttribute("msg", "抱歉,帐号或密码不对!");
return new ModelAndView(getLoginPage(),map);
}
}
}
----------------------------------------------------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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<!-- 自动搜索@Controller标注的类用于指明系统从哪个路径下寻找controller,然后提前初始化这些对象。-->
<!-- <context:component-scan base-package="com.jb.jubmis.action" /> -->
<!-- <context:component-scan base-package="com.comingx.wf.controller" /> -->
<!--上传控制-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8">
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--接受请求转入控制器,jsp调用的路径 -->
<prop key="excel.do">excelAction</prop>
<prop key="upload.do">uploadAction</prop>
<prop key="pubuser.do">pubuserAction</prop>
</props>
</property>
</bean>
<!--定义视图 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView
</value>
</property>
<!--所有的jsp页面都放在根目录的jsp文件夹中 -->
<!--
<property name="prefix">
<value>/jsp/</value>
</property> -->
<!--所有jsp的后缀都是.jsp -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!--多动作解析 -->
<bean id="paraMethodResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName">
<value>method</value>
</property>
</bean>
<!--dao -->
<bean id="excelDao" class="com.jb.jubmis.Dao.DaoImpl.ExcelDaoImpl"><!-- 访问类 -->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="pubUserDao" class="com.jb.jubmis.Dao.DaoImpl.PubUserDaoImpl"><!-- 访问类 -->
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- services -->
<bean id="excelServices" class="com.jb.jubmis.Services.impl.ExcelServicesImpl"><!-- 访问类 -->
<property name="excelDao" ref="excelDao" />
</bean>
<bean id="pubUserServices" class="com.jb.jubmis.Services.impl.PubUserServicesImpl"><!-- 访问类 -->
<property name="pubUserDao" ref="pubUserDao" />
</bean>
<!--action-->
<bean id="excelAction" class="com.jb.jubmis.action.ExcelAction"><!-- 访问类 -->
<property name="excelServices" ref="excelServices" />
<property name="methodNameResolver" ref="paraMethodResolver"/>
<!--定义要返回的页面 -->
<property name="viewPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="viewExcelPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="successPage">
<value>success</value>
</property>
</bean>
<!--pubuserAction-->
<bean id="pubuserAction" class="com.jb.jubmis.action.PubuserAction"><!-- 自己写的类 -->
<property name="pubUserServices" ref="pubUserServices" />
<property name="excelServices" ref="excelServices" />
<property name="methodNameResolver" ref="paraMethodResolver"/>
<!--定义要返回的页面 -->
<property name="indexPage">
<value>index</value>
</property>
<!--定义要返回的页面 -->
<property name="loginPage">
<value>login</value>
</property>
<!--定义要返回的页面 -->
<property name="registerPage">
<value>login</value>
</property>
</bean>
<bean id="uploadAction" class="com.jb.jubmis.action.UploadFileAction"><!-- 访问类 -->
<!--定义要返回的页面 -->
<property name="viewPage">
<value>input</value>
</property>
</bean>
<!--定义事务处理类 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jubmis" />
<property name="jpaProperties">
<props></props>
</property>
</bean>
<!-- <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jubmis" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
事物管理
<tx:annotation-driven transaction-manager="transactionManager" /> -->
<!--dao -->
<!-- 事物通知 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save.*" propagation="REQUIRED"/>
<tx:method name="in.*" propagation="REQUIRED"/>
<tx:method name="delete.*" propagation="REQUIRED"/>
<tx:method name="get.*" read-only="true"/>
<tx:method name="find.*" read-only="true"/>
<tx:method name="query.*" read-only="true"/>
</tx:attributes>
</tx:advice>
注入
<aop:config>
<aop:pointcut expression="execution(* com.jb.jubmis.Services.impl.*.*(..))" id="mycut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config> -->
</beans>
-----------------------------------------------------------persistence.xml----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="jubmis" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<!-- 实体 -->
<class>com.jb.jubmis.entity.Xskh</class>
<class>com.jb.jubmis.entity.Pubuser</class>
<!-- 连接数据库 -->
<properties>
<property name = "toplink.jdbc.driver" value = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name = "toplink.jdbc.url" value = "jdbc:sqlserver://svr:2433;DatabaseName=eqmis"/>
<property name = "toplink.jdbc.user" value = "sa"/>
<property name = "toplink.jdbc.password" value = "1.0"/>
</properties>
</persistence-unit>
</persistence>
-----------------------------------------------------------web.xml----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/login.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/login.jsp</location>
</error-page>
<error-page>
<exception-type>com.microsoft.sqlserver.jdbc.SQLServerException</exception-type>
<location>/login.jsp</location>
</error-page>
<servlet>
<servlet-name>dispather</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--applicationContext.xml的路径在src下的META-INF文件夹下-->
<param-value>/WEB-INF/classes/META-INF/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispather</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
----------------------------------------------------jsp---------------------------------------------------------
<form method="post" id="frmlogin" action="pubuser.do?method=login">
<p class="login_fm">
<label>帐 号:</label>
<input type="text" value="" name="acode" id="loginame">
</p>
<p class="login_fm">
<label>密 码:</label>
<input type="password" name="pass" id="loginpwd">
</p><br/><br/>
<p>
<img src="data:images/btn_login.jpg" alt="提交" onclick="settiemoutlogin('login')"/>
<img src="data:images/clear.jpg" alt="清空" onclick="clearlogin('loginame','loginpwd')"/>
</p>
</form>
SpringMVC+JPA使用注入的方式环境搭建的更多相关文章
- SpringMVC3.2+JPA使用注解的方式环境搭建
==============================entity=========================================================package ...
- spring+springMVC+mybatis的框架项目基础环境搭建
上一个项目在后台用到spring+springMVC+mybatis的框架,先新项目初步需求也已经下来,不出意外的话,应该也是用这个框架组合. 虽然在之前activiti相关的学习中所用到的框架也是这 ...
- SpringData JPA的学习笔记之环境搭建
一.环境搭建 1.加入jar包 spring jar+jpa jar +springData jar >>SpringData jar包 2.配置applicationCont ...
- Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建
1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...
- SpringMVC+Mybatis+Mysql实战项目学习--环境搭建
1.开发IDE:Spring Tool Suite(自带maven插件) 下载地址https://spring.io/tools/sts/all 在STS.ini配置信息中加下面一行 保证编码格式为u ...
- springmvc环境搭建以及常见问题解决
1.新建maven工程 a) 打开eclipse,file->new->project->Maven->Maven Project b) 下一步 c) 选择创建的工程为 ...
- SpringMVC环境搭建和详解
1.Spring容器和SpringMVC容器是父子容器 1.1 SpringMVC容器可以调用Spring容器中的所有内容 1.2 图示 2.SpringMVC环境搭建 1.导入jar包 2.在web ...
- springmvc+spring-data-jpa+hibernate环境搭建与配置
1.JPA诞生的缘由是为了整合第三方ORM框架,建立一种标准的方式,百度百科说是JDK为了实现ORM的天下归一,目前也是在按照这个方向发展,但是还没能完全实现.在ORM框架中,Hibernate是一支 ...
- SpringBoot系列教程JPA之基础环境搭建
JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Spring Data JPA是在 Hibernat ...
随机推荐
- poj 1465 Multiple(bfs+余数判重)
题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...
- 纯CSS3大转盘抽奖(响应式、可配置)
源于前段时候微信小程序最初火爆公测时段,把以前用 Canvas 实现的大转盘抽奖移植成微信小程序,无奈当时小程序对 Canvas 支持不够完善,只好降低用 CSS3 实现.虽然比不上 Canvas 绘 ...
- github.io hexo 安装
/***************************************************************** * github.io hexo 安装 * 说明: * 本文记录h ...
- ORACLE解锁record is locked by another user
在操作ORACLE数据库的时候,由于执行完,没有COMMIT,直接把PL/SQL关闭掉,后来导致那张表被锁住,当编辑时就会出现这个信息,record is locked by another user ...
- xp关不了机
自己测试:按第2点的设置既可成功1.点“开始→设置→控制面板→电源选项→高级电源管理”,如果你的机器支持高级电源管理功能,则选中“启用高级电源管理支持”:2.单击“开始”,并运行“regedit”,然 ...
- 【英语】Bingo口语笔记(78) - let系列
- Oracle RAC 负载均衡测试(结合服务器端与客户端)
Oracle RAC 负载均衡使得从客户端发起的连接能够有效地分配到监听器负载较小的实例上.有两种方式实现客户端负载均衡,一是通过配置客户端的load_balance,一是通过配置服务器端的remot ...
- node前端自动化
一.前端自动化-项目构建 我们平时写代码,喜欢建一个project,然后里面是css.js.images文件,以及index.html,而node可以自动化构建好项目,如下: /** * Create ...
- org.hibernate.MappingException: duplicate import异常
在开发hibernate时,一起多谢ORM类和映射文件时,报出:org.hibernate.MappingException: duplicate import com.XXX异常 解决方案: 检查你 ...
- MVC2.0前置
.NET MVC执行过程: 1.网址路由比对 2.执行Controller与Action 3.执行View并返回结果 在使用MVC中是由IgnoreRoute()辅助方法对比成功的,会导致程序直接跳离 ...