转自:https://blog.csdn.net/thinkingcao/article/details/52472252
C
所用到的jar包
数据库表
数据库表就不用教大家了,一张表,很简单的,下面是我建好的表
1.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="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"> 3 <display-name>json_test</display-name> 4 <welcome-file-list> 5 <welcome-file>login.jsp</welcome-file> 6 </welcome-file-list> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:spring/spring-*.xml</param-value> 10 </context-param> 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 <!-- 清理缓存 --> 15 <listener> 16 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 17 </listener> 18 <servlet> 19 <servlet-name>springMVC</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <init-param> 22 <param-name>contextConfigLocation</param-name> 23 <param-value>classpath:spring/spring-mvc.xml</param-value> 24 </init-param> 25 <load-on-startup>1</load-on-startup> 26 </servlet> 27 <servlet-mapping> 28 <servlet-name>springMVC</servlet-name> 29 <url-pattern>*.do</url-pattern> 30 </servlet-mapping> 31 <filter> 32 <filter-name>encodingFilter</filter-name> 33 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 34 <init-param> 35 <param-name>encoding</param-name> 36 <param-value>UTF-8</param-value> 37 </init-param> 38 <init-param> 39 <param-name>forceEncoding</param-name> 40 <param-value>true</param-value> 41 </init-param> 42 </filter> 43 <filter-mapping> 44 <filter-name>encodingFilter</filter-name> 45 <url-pattern>/*</url-pattern> 46 </filter-mapping> 47 <filter> 48 <filter-name>openSession</filter-name> 49 <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 50 </filter> 51 <filter-mapping> 52 <filter-name>openSession</filter-name> 53 <url-pattern>/*</url-pattern> 54 </filter-mapping> 55 </web-app>
2.spring-comon.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:cache="http://www.springframework.org/schema/cache" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 7 xmlns:jee="http://www.springframework.org/schema/jee" 8 xmlns:jms="http://www.springframework.org/schema/jms" 9 xmlns:lang="http://www.springframework.org/schema/lang" 10 xmlns:mvc="http://www.springframework.org/schema/mvc" 11 xmlns:oxm="http://www.springframework.org/schema/oxm" 12 xmlns:task="http://www.springframework.org/schema/task" 13 xmlns:tx="http://www.springframework.org/schema/tx" 14 xmlns:util="http://www.springframework.org/schema/util" 15 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 16 xsi:schemaLocation="http://www.springframework.org/schema/beans 17 http://www.springframework.org/schema/beans/spring-beans.xsd 18 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 19 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd 20 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 21 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 22 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 23 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd 24 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd 25 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 26 http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd 27 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd 28 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 29 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 30 31 <!--扫描映射 --> 32 <context:component-scan base-package="com.ssh"></context:component-scan> 33 34 <!-- 引入property配置文件 --> 35 <context:property-placeholder location="classpath:prop/jdbc.properties"></context:property-placeholder> 36 <!-- 配置数据源 --> 37 <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> 38 <property name="driverClassName" value="${jdbc.mysql.driverClassName}"></property> 39 <property name="url" value="${jdbc.mysql.url}"></property> 40 <property name="username" value="${jdbc.mysql.username}"></property> 41 <property name="password" value="${jdbc.mysql.password}"></property> 42 <!-- 初始化连接大小 43 <property name="initialSize" value="${jdbc.initialSize}"></property> --> 44 <!-- 连接池最大数量 45 <property name="maxActive" value="${jdbc.maxActive}"></property>--> 46 <!-- 连接池最大空闲 --> 47 <!-- <property name="maxIdle" value="${maxIdle}"></property> --> 48 <!-- 连接池最小空闲 49 <property name="minIdle" value="${jdbc.minIdle}"></property>--> 50 <!-- 获取连接最大等待时间 51 <property name="maxWait" value="${jdbc.maxWait}"></property>--> 52 </bean> 53 54 <!-- 配置SessionFactory --> 55 <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"> 56 <property name="dataSource" ref="dataSource"></property> 57 <property name="hibernateProperties"> 58 <props> 59 <prop key="hibernate.dialect">${jdbc.mysql.dialect}</prop> 60 <prop key="hibernate.hbm2ddl.auto">update</prop> 61 <!--是否显示sql语句 我在这里是显示的 --> 62 <prop key="hibernate.show_sql">true</prop> 63 <!--格式化显示sql语句 --> 64 <prop key="hibernate.format_sql">true</prop> 65 </props> 66 </property> 67 <!-- 自动扫描制定位置下的实体进行映射 --> 68 <property name="packagesToScan" value="com.ssh.entity"></property> 69 </bean> 70 71 <!-- 配置一个事务管理器 --> 72 <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager"> 73 <property name="sessionFactory" ref="sessionFactory"> 74 </property></bean> 75 76 <!-- 应该是开启事物 --> 77 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> 78 </beans>
3.spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans 3 http://www.springframework.org/schema/beans/spring-beans.xsd 4 http://www.springframework.org/schema/context 5 http://www.springframework.org/schema/context/spring-context-3.2.xsd 6 http://www.springframework.org/schema/mvc 7 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 8 9 <!-- 注解扫描包 --> 10 <context:component-scan base-package="com.ssh"> 11 12 <!-- 开启注解 --> 13 <mvc:annotation-driven> 14 15 <!-- 定义视图解析器 --> 16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> 17 <property name="prefix" value="/"></property> 18 <property name="suffix" value=".jsp"></property> 19 </bean> 20 </mvc:annotation-driven></context:component-scan></beans>
4. jdbc.properties
1 # JDBC 2 # u8BBEu7F6Eu8FDEu63A5u6C60u8FDEu63A5u65F6u7684u6570u91CF 3 jdbc.initialSize=1 4 jdbc.filters=stat 5 # u8FDEu63A5u6C60u4E2Du5B58u5728u7684u6700u5C0Fu8FDEu63A5u6570u76EEu3002u8FDEu63A5u6C60u4E2Du8FDEu63A5u6570u76EEu53EFu4EE5u53D8u5F88u5C11uFF0Cu5982u679Cu4F7Fu7528u4E86maxAgeu5C5Eu6027uFF0Cu6709u4E9Bu7A7Au95F2u7684u8FDEu63A5u4F1Au88ABu5173u95EDu56E0u4E3Au79BBu5B83u6700u8FD1u4E00u6B21u8FDEu63A5u7684u65F6u95F4u8FC7u53BBu592Au4E45u4E86u3002u4F46u662FuFF0Cu6211u4EECu770Bu5230u7684u6253u5F00u7684u8FDEu63A5u4E0Du4F1Au5C11u4E8EminIdleu3002 6 jdbc.minIdle=1 7 # u8FDEu63A5u6570u636Eu5E93u7684u6700u5927u8FDEu63A5u6570u3002u8FD9u4E2Au5C5Eu6027u7528u6765u9650u5236u8FDEu63A5u6C60u4E2Du80FDu591Fu6253u5F00u8FDEu63A5u7684u6570u91CFuFF0Cu53EFu4EE5u65B9u4FBFu6570u636Eu5E93u505Au8FDEu63A5u5BB9u91CFu89C4u5212u3002 8 jdbc.maxActive=99 9 jdbc.maxWait=1000 10 jdbc.minEvictableIdleTimeMillis=300000 11 jdbc.poolPreparedStatements=true 12 jdbc.maxPoolPreparedStatementPerConnectionSize=50 13 jdbc.timeBetweenEvictionRunsMillis=60000 14 jdbc.validationQuery=select 1 from dual 15 16 jdbc.removeAbandonedTimeout=150 17 jdbc.logAbandoned=true 18 jdbc.removeAbandoned=true 19 jdbc.testOnBorrow=false 20 jdbc.testOnReturn=false 21 22 23 #ORACLE u6570u636Eu5E93u8FDEu63A5u65B9u5F0F 24 #jdbc.oracle.driverClassName=oracle.jdbc.driver.OracleDriver 25 #jdbc.oracle.url=jdbc:oracle:thin:@localhost:1521:orcl 26 #jdbc.oracle.username=wrg 27 #jdbc.oracle.password=wrg 28 #jdbc.oracle.dialect=org.hibernate.dialect.Oracle10gDialect 29 30 31 32 # MYSQL u6570u636Eu5E93u8FDEu63A5u65B9u5F0F 33 jdbc.mysql.driverClassName=com.mysql.jdbc.Driver 34 jdbc.mysql.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 35 jdbc.mysql.username=root 36 jdbc.mysql.password=123456 37 jdbc.mysql.dialect=org.hibernate.dialect.MySQL5InnoDBDialect 38 39 40 #HIBERNATE 41 jdbc.show_sql=false 42 jdbc.format_sql=false
5.
log4j.properties
1 #console log 2 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 3 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 4 log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c - %m%n 5 6 7 #logger 8 log4j.logger.org.springframework=DEBUG,CONSOLE 9 log4j.logger.org.hibernate=INFO,CONSOLE 10 log4j.logger.org.apache=INFO,CONSOLE 11 12 13 14 log4j.rootLogger=DEBUG,CONSOLE
6.
1 package com.ssh.entity; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 import org.hibernate.annotations.GenericGenerator; 10 11 @Entity 12 @Table(name="TUSER") 13 public class User { 14 @Id 15 @GeneratedValue(generator="id") 16 @GenericGenerator(name="id",strategy="identity") 17 private Integer id; 18 private String name; 19 private String password; 20 @Column(name="LOGIN_DATE") 21 private String loginDate; 22 public Integer getId() { 23 return id; 24 } 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 public String getName() { 29 return name; 30 } 31 public void setName(String name) { 32 this.name = name; 33 } 34 public String getPassword() { 35 return password; 36 } 37 public void setPassword(String password) { 38 this.password = password; 39 } 40 public String getLoginDate() { 41 return loginDate; 42 } 43 public void setLoginDate(String loginDate) { 44 this.loginDate = loginDate; 45 } 46 @Override 47 public String toString() { 48 return "User [id=" + id + ", name=" + name + ", password=" + password 49 + ", loginDate=" + loginDate + "]"; 50 } 51 52 53 54 }
7.
1 package com.ssh.dao; 2 3 import java.util.List; 4 5 import com.ssh.entity.User; 6 7 public interface UserDao { 8 // 登录 9 User selectUser(User user) throws Exception; 10 11 // 查询所有 12 List getAllUsers() throws Exception; 13 14 // 添加用户 15 void addUser(User user) throws Exception; 16 17 // 删除用户 18 void delUser(Integer id) throws Exception; 19 20 // 修改用户 21 void updateUser(User user) throws Exception; 22 23 // 单个查询 24 User getUser(Integer id) throws Exception; 25 }
8.
1 package com.ssh.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Repository; 10 11 import com.ssh.entity.User; 12 13 @Repository 14 @SuppressWarnings("unchecked") 15 public class UserDaoImpl implements UserDao{ 16 17 @Autowired 18 private SessionFactory sessionFactory; 19 20 21 //登录 22 public User selectUser(User user) throws Exception { 23 Query query = sessionFactory.getCurrentSession().createQuery("from User u where u.name=? and u.password=?"); 24 query.setString(0, user.getName()); 25 query.setString(1, user.getPassword()); 26 List list = query.list(); 27 if(list==null||list.size()==0){ 28 throw new RuntimeException("查询失败"); 29 } 30 return (User) list.get(0); 31 } 32 33 //查询所有 34 public List getAllUsers() throws Exception { 35 Query query = sessionFactory.getCurrentSession().createQuery("from User"); 36 List list = query.list(); 37 return list; 38 } 39 40 //单个查询 41 public User getUser(Integer id) throws Exception { 42 return (User) sessionFactory.getCurrentSession().createQuery("from User u where u.id ="+id).uniqueResult(); 43 } 44 45 //添加用户 46 public void addUser(User user) throws Exception { 47 System.out.println("11111111111111111"+user.getName()); 48 sessionFactory.getCurrentSession().save(user); 49 } 50 51 //删除用户 52 public void delUser(Integer id) throws Exception { 53 sessionFactory.getCurrentSession().createQuery("delete User u where u.id="+id).executeUpdate(); 54 55 } 56 57 //修改用户 58 public void updateUser(User user) throws Exception { 59 Session session = sessionFactory.getCurrentSession(); 60 session.beginTransaction(); 61 String hql = ("update User u set u.name = ?,u.password = ?,u.loginDate = ? where u.id = ?"); 62 Query query = session.createQuery(hql); 63 query.setParameter(0, user.getName()); 64 query.setParameter(1, user.getPassword()); 65 query.setParameter(2, user.getLoginDate()); 66 query.setParameter(3, user.getId()); 67 query.executeUpdate(); 68 session.getTransaction().commit(); 69 } 70 }
9.
1 package com.ssh.service; 2 3 import java.util.List; 4 5 import com.ssh.entity.User; 6 7 public interface UserService { 8 // 登录 9 User selectUser(User user) throws Exception; 10 11 // 查询所有 12 List getAllUsers() throws Exception; 13 14 // 添加用户 15 void addUser(User user) throws Exception; 16 17 // 删除用户 18 void delUser(Integer id) throws Exception; 19 20 // 修改用户 21 void updateUser(User user) throws Exception; 22 23 // 单个查询 24 User getUser(Integer id) throws Exception; 25 }
10.
1 package com.ssh.service; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 8 import com.ssh.dao.UserDao; 9 import com.ssh.entity.User; 10 @Service("userService") 11 public class UserServiceImpl implements UserService { 12 @Autowired 13 private UserDao userDao; 14 //登录 15 public User selectUser(User user) throws Exception { 16 return userDao.selectUser(user); 17 } 18 19 //单个查询 20 public User getUser(Integer id) throws Exception { 21 return userDao.getUser(id); 22 } 23 //查询所有 24 public List getAllUsers() throws Exception { 25 List users = userDao.getAllUsers(); 26 return users; 27 } 28 29 //添加用户 30 public void addUser(User user) throws Exception { 31 userDao.addUser(user); 32 } 33 //删除用户 34 35 public void delUser(Integer id) throws Exception { 36 userDao.delUser(id); 37 } 38 //修改用户 39 public void updateUser(User user) throws Exception { 40 userDao.updateUser(user); 41 } 42 }
11.
1 package com.ssh.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod; 10 11 import com.ssh.entity.User; 12 import com.ssh.path.Path; 13 import com.ssh.service.UserService; 14 15 @Controller 16 @RequestMapping(value=Path.USER) 17 public class UserAction { 18 @Autowired 19 private UserService userService; 20 @RequestMapping(value=Path.LOGIN_INDEX) 21 public String login(HttpServletRequest request, HttpServletResponse response) throws Exception{ 22 return Path.LOGIN_INDEX; 23 } 24 25 @RequestMapping(value=Path.LOGIN_OK,method=RequestMethod.POST) 26 public String loginCheck(User user,HttpServletRequest request, HttpServletResponse response) throws Exception{ 27 response.setContentType("text/html;charset=utf-8"); 28 User u = userService.selectUser(user); 29 System.out.println("user is ------------------------ "+u); 30 request.setAttribute("user", u); 31 return "redirect:/user/index.do"; 32 } 33 34 //单个查询 35 @RequestMapping(value=Path.GET_USER) 36 public String getUser(Integer id,HttpServletRequest request) throws Exception{ 37 request.setAttribute("user", userService.getUser(id)); 38 return Path.UPDATE_USER; 39 } 40 41 //查询所有 42 @RequestMapping(value=Path.INDEX) 43 public String getAllUsers(HttpServletRequest request) throws Exception{ 44 request.setAttribute("userList", userService.getAllUsers()); 45 return Path.INDEX; 46 } 47 //添加跳转方法 48 @RequestMapping(value=Path.TO_ADDUSER) 49 public String toAddUser(){ 50 return Path.ADD_USER; 51 } 52 //添加用户 53 @RequestMapping(value=Path.ADD_USER) 54 public String addUser(User user,HttpServletRequest request) throws Exception{ 55 System.out.println("用户名:======"+user.getName()); 56 userService.addUser(user); 57 return "redirect:/user/index.do"; 58 } 59 60 //删除用户 61 @RequestMapping(value=Path.DEL_USER) 62 public String delUser(Integer id,HttpServletResponse response) throws Exception{ 63 userService.delUser(id); 64 return "redirect:/user/index.do"; 65 } 66 67 //更新用户 68 @RequestMapping(value=Path.UPDATE_USER) 69 public String updateUser(User user,HttpServletRequest request) throws Exception{ 70 71 userService.updateUser(user); 72 user = userService .getUser(user.getId()); 73 request.setAttribute("user", user); 74 return "redirect:/user/index.do"; 75 76 } 77 }
12.
1 package com.ssh.test; 2 3 import org.hibernate.SessionFactory; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import com.ssh.dao.UserDao; 12 import com.ssh.entity.User; 13 import com.ssh.service.UserService; 14 15 @RunWith(SpringJUnit4ClassRunner.class) 16 @ContextConfiguration(locations="classpath:spring/spring-common.xml") 17 @Transactional 18 public class TestAll { 19 @Autowired 20 private SessionFactory sessionFactory; 21 /** 22 * 测试sessionfactory 23 * 测试时 spring-common 不能存在 事物bean 24 * 不能存在 事物管理器 bean 25 * 不能存在dao 26 * 不能存在service 27 * 不能存在action 28 * 只是为了防止当其他内容写错时 sessionfactory也开启不了 除非是其他的bean没有错 29 */ 30 @Test 31 public void testSf(){ 32 System.out.println("测试开启"); 33 System.out.println(" sessionfactory = "+sessionFactory); 34 System.out.println("测试完成"); 35 } 36 /** 37 * 测试UserDao 38 */ 39 @Autowired 40 private UserDao userDao; 41 @Test 42 public void testUserDao() throws Exception{ 43 User u = new User(); 44 u.setName("admin"); 45 u.setPassword("12345678"); 46 User user = userDao.selectUser(u); 47 System.err.println("user is "+user); 48 userDao.addUser(u); 49 } 50 /** 51 * 测试UserService 52 */ 53 @Autowired 54 private UserService userService; 55 @Test 56 public void testUserService() throws Exception{ 57 User u = new User(); 58 u.setName("admin"); 59 u.setPassword("12345678"); 60 User user = userService.selectUser(u); 61 System.out.println("user is "+user); 62 } 63 }
13.
1 package com.ssh.path; 2 3 public class Path { 4 public static final String USER = "/user"; 5 /** 6 * 登陆 7 */ 8 public static final String LOGIN_INDEX = "/login"; 9 /** 10 * 登陆成功 11 */ 12 public static final String LOGIN_OK = "/loginok"; 13 14 /** 15 * 查询所有 16 */ 17 public static final String INDEX = "/index"; 18 /** 19 * 添加用户 20 */ 21 public static final String ADD_USER = "/addUser"; 22 23 /** 24 * 跳转添加用户 25 */ 26 public static final String TO_ADDUSER = "/toaddUser"; 27 28 /** 29 * 删除用户 30 */ 31 public static final String DEL_USER = "/delUser"; 32 33 /** 34 * 更新用户 35 */ 36 public static final String UPDATE_USER = "/updateUser"; 37 38 /** 39 * 跳转更新用户 40 */ 41 public static final String GET_USER = "/getUser"; 42 }
14.
1 package com.ssh.test; 2 3 import org.hibernate.SessionFactory; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import com.ssh.dao.UserDao; 12 import com.ssh.entity.User; 13 import com.ssh.service.UserService; 14 15 @RunWith(SpringJUnit4ClassRunner.class) 16 @ContextConfiguration(locations="classpath:spring/spring-common.xml") 17 @Transactional 18 public class TestAll { 19 @Autowired 20 private SessionFactory sessionFactory; 21 /** 22 * 测试sessionfactory 23 * 测试时 spring-common 不能存在 事物bean 24 * 不能存在 事物管理器 bean 25 * 不能存在dao 26 * 不能存在service 27 * 不能存在action 28 * 只是为了防止当其他内容写错时 sessionfactory也开启不了 除非是其他的bean没有错 29 */ 30 @Test 31 public void testSf(){ 32 System.out.println("测试开启"); 33 System.out.println(" sessionfactory = "+sessionFactory); 34 System.out.println("测试完成"); 35 } 36 /** 37 * 测试UserDao 38 */ 39 @Autowired 40 private UserDao userDao; 41 @Test 42 public void testUserDao() throws Exception{ 43 User u = new User(); 44 u.setName("admin"); 45 u.setPassword("12345678"); 46 User user = userDao.selectUser(u); 47 System.err.println("user is "+user); 48 userDao.addUser(u); 49 } 50 /** 51 * 测试UserService 52 */ 53 @Autowired 54 private UserService userService; 55 @Test 56 public void testUserService() throws Exception{ 57 User u = new User(); 58 u.setName("admin"); 59 u.setPassword("12345678"); 60 User user = userService.selectUser(u); 61 System.out.println("user is "+user); 62 } 63 }
springmvc+spring3+hibernate4框架简单整合,简单实现增删改查功能