SSH:
S:SpringMVC —– S:Spring —– H:Hibernate
引入的包:
antlr-2.7.7.jar
aopalliance.jar
asm-3.3.1.jar
aspectjrt.jar
aspectjweaver.jar
c3p0-0.9.1.2.jar
cglib-2.2.2.jar
classmate-1.3.0.jar
commons-codec-1.10.jar
commons-dbcp2-2.1.1.jar
commons-dbutils-1.6.jar
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-logging-1.2.jar
commons-pool2-2.4.2.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.10.Final.jar
hibernate-envers-5.2.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-jpamodelgen-5.2.10.Final.jar
jandex-2.0.3.Final.jar
javassist-3.20.0-GA.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.1.Final.jar
jsp-api.jar
jstl-1.2.jar
mysql-connector-java-5.1.7-bin.jar
servlet-api.jar
spring-aop-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-context-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
spring-jdbc-4.3.7.RELEASE.jar
spring-orm-4.3.7.RELEASE.jar
spring-tx-4.3.7.RELEASE.jar
spring-web-4.3.7.RELEASE.jar
spring-webmvc-4.3.7.RELEASE.jar
配置文件:
web.xml
<!-- Spring 相关配置 --> <!-- needed for ContextLoaderListener --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/config/bean-*.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC --> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <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>
bean-base.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--将Hibernate中的连接数据库的相关信息配置到Spring中来 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="acquireIncrement" value="2"></property> <property name="maxPoolSize" value="100"></property> <property name="minPoolSize" value="2"></property> <property name="maxStatements" value="100"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test01"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> <!--Spring和Hibernate整合的关键点是SessionFactory的创建问题 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--下面我要将hibernate的配置文件写到这个里面来--> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="current_session_context_class">thread</prop> </props> </property> <!--编写hibernate的映射配置--> <property name="mappingDirectoryLocations"> <list> <!--配置mapping映射文件的根路径就好了--> <value>classpath:com/wc/hrm/pojo</value> </list> </property> </bean> <!--配置aop的扫描--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
spring-mvc.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.3.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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置的是文件上传的类 前面的文件名字必须是 multipartResolver 别的名字好像不行--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--配置IOC/DI的扫描器--> <context:component-scan base-package="com.wc.hrm"> </context:component-scan> <!-- 设置mvc的注解的驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置事务 --> <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="dataSource" ref="dataSource"></property> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--应用这个事务--> <tx:annotation-driven transaction-manager="txManager"/> </beans>
User.java
/** * 实体管理员类 * @author Administrator * */ public class User { private Integer id;//管理员id private String loginName;//登录名 private String password;//密码 private int status;//状态 private String username;//用户名 private String createDate;//创建时间 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } public User(String loginName, String password, int status, String username) { super(); this.loginName = loginName; this.password = password; this.status = status; this.username = username; } public User() { super(); } @Override public String toString() { return "User [id=" + id + ", loginName=" + loginName + ", password=" + password + ", status=" + status + ", username=" + username + ", createDate=" + createDate + "]"; } }
User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!--package是否可以省略? 可以 省略之后类的名字 编写全路径就OK了 --> <hibernate-mapping package="com.wc.hrm.pojo" default-lazy="false"> <!--一个class就相当于对应了一张表 name:表示的是我们对应数据库表的那个对象的名字 这个名字如果上面没有配置package的话那么是需要全路径的 table:这个标签是我们的JAVA对象 在数据库表中对应的这个表名 abstract:这个用在继承映射的时候 这个类是否在数据库对应表的问题 lazy:是否这个对象支持懒惰形加载 (我们只有在使用这个数据的时候那么 才加载这个数据 如果我们不需要使用这个数据的话 那么就不会加载) abstract="true" lazy="true" table:是否可以省略? 可以的 省略之后默认的表明就是实体的名字 --> <class name="User" table="u_user"> <!--配置的主键 id:是用来配置这个主键的(每一个表都有主键) 值直接写我们实体里面的主键属性 column:这个是用来定义这个列的列名的 type:表示的是当前的这个类中的属性的数据格式是什么 这里可以写java里面的类型的全路径 也可以写hibernate中支持的数据类型 column:可以省略 默认的字段就是属性的名字 type:也可以省略:默认的类型和实体的数据类型是一样的 --> <id name="id" column="ID"> <generator class="native"></generator> </id> <!--配置的是普通的列 property:设置的是普通的列 column:列名(当前对象的属性在数据库中对应的列名) 可以省略 lazy:是否支持懒惰性的加载 :也默认值 not-null:是否可以为null unique:是否这个列的值是唯一的 type:可以省略 默认数据类型和实体中的属性的类型一直 length:设置的是长度(有点必须的)--> <property name="loginName" column="loginname"></property> <property name="password" column="PASSWORD"></property> <property name="status" column="STATUS"></property> <property name="username"></property> <property name="createDate"></property> </class> </hibernate-mapping>
IUserDao.java
/** * 持久层接口:管理员 * @author Administrator * */ public interface IUSerDao { /** * 增加方法 * @param user * @throws Exception */ public void insert(User user)throws Exception; /** * 修改方法 * @param user * @throws Exception */ public void update(User user) throws Exception; /** * 删除方法 * @param id * @throws Exception */ public void delete(Integer id)throws Exception; /** * id查询方法 * @param id * @return * @throws Exception */ public User query(Integer id )throws Exception; /** * username,status查询方法 * @param username * @param status * @return * @throws Exception */ public List<User> query(String username,Integer status) throws Exception ; /** * 登录查询方法 * @param username * @param password * @return * @throws Exception */ public User query(String username,String password) throws Exception ; }
UserDao.java
/** * 持久层实现:管理员 * * @author Administrator * */ @Repository public class UserDao implements IUSerDao { @Autowired private SessionFactory sessionFactory = null; @Override public void insert(User user) throws Exception { user.setCreateDate(DateUtils.getDate3());// 设置创建时间(工具类) sessionFactory.openSession().save(user); } @Override public void update(User user) throws Exception { Session session = sessionFactory.getCurrentSession(); session.update(user); } @Override @Transactional public void delete(Integer id) throws Exception { Session session = sessionFactory.getCurrentSession(); Query query = session.createQuery("delete from User as u where u.id=:id"); query.setParameter("id", id); query.executeUpdate(); System.out.println("delete"); } @Override public User query(Integer id) throws Exception { Session session = sessionFactory.openSession(); User user = session.get(User.class, id); return user; } @SuppressWarnings("rawtypes") @Override public User query(String loginname, String password) throws Exception { Session session = sessionFactory.openSession(); System.out.println(loginname); System.out.println(password); Query query = session.createQuery("select u from User u where u.loginName=:username AND u.password=:password"); query.setParameter("username", loginname); query.setParameter("password", password); User user = null; if (query.list().size() != 0) { user = (User) query.list().get(0); } return user; } @Override public List<User> query(String username, Integer status) throws Exception { Session session = sessionFactory.openSession(); Query query = null; String hql = "select u from User u where 1=1"; if (username == null || username == "" || username == " ") { if (status != null) { hql = hql + " AND u.status=:status"; query = session.createQuery(hql); query.setParameter("status", status); } else { query = session.createQuery(hql); } } else { if (status != null) { hql = hql + " AND u.status=:status AND u.username like :username"; query = session.createQuery(hql); query.setParameter("username", "%" + username + "%"); query.setParameter("status", status); } else { hql = hql + " AND u.username like :username"; query = session.createQuery(hql); query.setParameter("username", "%" + username + "%"); } } return query.list(); } }
IUserService.java
/** * 业务逻辑层接口:管理员 * @author Administrator * */ public interface IUserService { /** * 增加方法 * @param employee * @throws Exception */ public void insert(User user)throws Exception; /** * 修改方法 * @param employee * @throws Exception */ public void update(User user) throws Exception; /** * 删除方法 * @param employee * @throws Exception */ public void delete(Integer id)throws Exception; /** * 查询方法 * @param id * @return * @throws Exception */ public User query(Integer id )throws Exception; /** * 查询方法 * @return * @throws Exception */ public List<User> query(String username,Integer status)throws Exception; /** * 查询方法 * @param username * @param password * @return * @throws Exception */ public User query(String username,String password )throws Exception; }
UserService.java
/** * 业务逻辑层实现:管理员 * @author Administrator * */ @Service @Transactional public class UserService implements IUserService{ @Autowired private IUSerDao userDao=null; @Override public void insert(User user) throws Exception { userDao.insert(user); } @Override public void update(User user) throws Exception { userDao.update(user); } @Override public void delete(Integer id) throws Exception { if(id!=null) { userDao.delete(id); } } @Override public User query(Integer id) throws Exception { User user=null; if(id!=null) { user=userDao.query(id); } return user; } @Override public User query(String loginname, String password) throws Exception { User user=userDao.query(loginname,password); System.out.println("service"+user); return user; } @Override public List<User> query(String username, Integer status) throws Exception { List<User> users=userDao.query(username,status); return users; } }
UserControll.java
/** * 控制层:管理员 * @author Administrator * */ @Controller @RequestMapping("/user") public class UserControll { @Resource private IUserService userService=null; /** * 用户查询 * @param username * @param status * @return * @throws Exception */ @RequestMapping("/selectUser.action") public ModelAndView selectUser(String username,Integer status) throws Exception { List<User> users=userService.query(username,status); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("user/user"); modelAndView.addObject("users",users); return modelAndView; } /** * 添加用户跳转 * @return */ @RequestMapping("/addUser.action") public String addUser() { return "user/showAddUser"; } /** * 添加用户提交 * @param user * @return * @throws Exception */ @RequestMapping("/addUserF.action") public ModelAndView addUser(User user) throws Exception { userService.insert(user); return selectUser(null,null); } /** * 修改用户跳转 * @param id * @return * @throws Exception */ @RequestMapping("/updateUser.action") public ModelAndView updateUser(int id) throws Exception { User user=userService.query(id); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("user/showUpdateUser"); modelAndView.addObject("user",user); return modelAndView; } /** * 修改用户提交 * @param user * @return * @throws Exception */ @RequestMapping("/updateUserF.action") public ModelAndView updateUser(User user) throws Exception { userService.update(user); return selectUser(null,null); } /** * 删除用户 * @return * @throws Exception */ @RequestMapping("/removeUser.action") public ModelAndView removeUser(int[] ids ) throws Exception { for(int i:ids) { userService.delete(i); } return selectUser(null,null); } }