工程目录结构:
依赖jar包:
vo: User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 package com.course.vo;import java.util.Date;public class User { private Integer id; private String name; private int age; private String address; private Date hireDate; public User () { super (); } public User (String name, int age, String address, Date hireDate) { super (); this .name = name; this .age = age; this .address = address; this .hireDate = hireDate; } public User (Integer id, String name, int age, String address, Date hireDate) { super (); this .id = id; this .name = name; this .age = age; this .address = address; this .hireDate = hireDate; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public String getAddress () { return address; } public void setAddress (String address) { this .address = address; } public Date getHireDate () { return hireDate; } public void setHireDate (Date hireDate) { this .hireDate = hireDate; } @Override public String toString () { return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", hireDate=" + hireDate + "]" ; } }
User.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package ="com.course.vo" > <class name ="User" table ="User" > <id name ="id" column ="id" > <generator class ="native" > </generator > </id > <property name ="name" column ="name" > </property > <property name ="age" column ="age" > </property > <property name ="address" column ="address" > </property > <property name ="hireDate" column ="hire_date" type ="java.util.Date" > </property > </class > </hibernate-mapping >
Dao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.course.dao;import java.util.List;import com.course.vo.User;public interface UserDao { public void addUser (User user) ; public void updateUser (User user) ; public void deleteUser (User user) ; public User queryUserById (Integer id) ; public List<User> queryAllUser () ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.course.dao.impl;import java.util.List;import org.springframework.orm.hibernate4.HibernateTemplate;import com.course.dao.UserDao;import com.course.vo.User;public class UserDaoImpl implements UserDao { private HibernateTemplate hibernateTemplate; public void setHibernateTemplate (HibernateTemplate hibernateTemplate) { this .hibernateTemplate = hibernateTemplate; } @Override public void addUser (User user) { hibernateTemplate.save(user); } @Override public void updateUser (User user) { hibernateTemplate.update(user); } @Override public void deleteUser (User user) { hibernateTemplate.delete(user); } @Override public User queryUserById (Integer id) { User user = hibernateTemplate.get(User.class, id); return user; } @Override public List<User> queryAllUser () { List<User> userList = (List<User>) hibernateTemplate.find("from User" ); return userList; } }
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.course.service;import java.util.List;import com.course.vo.User;public interface UserService { public void addUser (User user) ; public void updateUser (User user) ; public void deleteUser (User user) ; public User queryUserById (Integer id) ; public List<User> queryAllUser () ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.course.service.impl;import java.util.List;import com.course.dao.UserDao;import com.course.service.UserService;import com.course.vo.User;public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao (UserDao userDao) { this .userDao = userDao; } @Override public void addUser (User user) { userDao.addUser(user); } @Override public void updateUser (User user) { userDao.updateUser(user); } @Override public void deleteUser (User user) { userDao.deleteUser(user); } @Override public User queryUserById (Integer id) { User user = userDao.queryUserById(id); return user; } @Override public List<User> queryAllUser () { List<User> userList = userDao.queryAllUser(); return userList; } }
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.course.controller;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import com.course.service.UserService;import com.course.vo.User;public class UserController { private UserService userService; public void setUserService (UserService userService) { this .userService = userService; } public void addUser () { Date hireDate = new Date (); User user = new User ("name2" , 2 , "address2" , hireDate); userService.addUser(user); } public void updateUser () { SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ); Date hireDate = null ; try { hireDate = simpleDateFormat.parse("2005-01-01 08:30:20" ); } catch (ParseException e) { e.printStackTrace(); } User user = new User (169 , "name1" , 10 , "address100" , hireDate); userService.updateUser(user); } public void deleteUser () { User user = new User (); user.setId(170 ); userService.deleteUser(user); } public User queryUserById (Integer id) { return userService.queryUserById(id); } public List<User> queryAllUser () { return userService.queryAllUser(); } }
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 package com.test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.course.controller.UserController;import com.course.dao.UserDao;import com.course.vo.User;public class TestHibernateTemplate { public static void main (String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("classpath:applicationContext.xml" ); UserController userController = (UserController) applicationContext.getBean("userController" ); userController.addUser(); userController.updateUser(); userController.deleteUser(); User user = userController.queryUserById(167 ); System.out.println(user); List<User> userList = userController.queryAllUser(); System.out.println(userList); } }
注: HibernateTemplate.update()
和HibernateTemplate.delete()
方法是根据id操作相应记录的,所以vo对象必须设置id,否则会报错。而且id对应的要操作的记录必须存在,否则会报错。
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <hibernate-configuration > <session-factory > <property name ="dialect" > org.hibernate.dialect.MySQLDialect</property > <property name ="hbm2ddl.auto" > update</property > <property name ="show_sql" > true</property > <property name ="format_sql" > true</property > <mapping resource ="com/course/vo/User.hbm.xml" /> </session-factory > </hibernate-configuration >
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name ="driverClassName" value ="com.mysql.jdbc.Driver" > </property > <property name ="url" value ="jdbc:mysql://127.0.0.1:3306/spring_test" > </property > <property name ="username" value ="root" > </property > <property name ="password" value ="123456" > </property > </bean > <bean id ="sessionFactory" class ="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" > </property > <property name ="configLocations" value ="classpath:hibernate.cfg.xml" > </property > </bean > <bean id ="transactionManager" class ="org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name ="sessionFactory" ref ="sessionFactory" > </property > </bean > <tx:advice id ="advice" transaction-manager ="transactionManager" > <tx:attributes > <tx:method name ="add*" propagation ="REQUIRED" /> <tx:method name ="save*" propagation ="REQUIRED" /> <tx:method name ="insert*" propagation ="REQUIRED" /> <tx:method name ="update*" propagation ="REQUIRED" /> <tx:method name ="delete*" propagation ="REQUIRED" /> <tx:method name ="get*" propagation ="REQUIRED" /> <tx:method name ="query*" propagation ="REQUIRED" read-only ="true" /> <tx:method name ="*" propagation ="REQUIRED" read-only ="true" /> </tx:attributes > </tx:advice > <aop:config > <aop:pointcut id ="pc" expression ="execution(* com.course.service.impl.*.*(..))" /> <aop:advisor advice-ref ="advice" pointcut-ref ="pc" /> </aop:config > <bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate4.HibernateTemplate" > <property name ="sessionFactory" ref ="sessionFactory" > </property > </bean > <bean id ="userDao" class ="com.course.dao.impl.UserDaoImpl" > <property name ="hibernateTemplate" ref ="hibernateTemplate" > </property > </bean > <bean id ="userService" class ="com.course.service.impl.UserServiceImpl" autowire ="byType" > </bean > <bean id ="userController" class ="com.course.controller.UserController" autowire ="byType" > </bean > </beans >
注: 事务应该加在service层,不应该加在dao层
调整配置文件 拆分配置文件:
db.properties
application-dao.xml
application-service.xml
application-controller.xml
applicationContext.xml
1.db.properties
1 2 3 4 driver =com.mysql.jdbc.Driver url =jdbc:mysql://127.0.0.1:3306/spring_test user =root password =123456
2.application-dao.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <context:property-placeholder location ="classpath:db.properties" system-properties-mode ="FALLBACK" /> <bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name ="driverClassName" value ="${driver}" > </property > <property name ="url" value ="${url}" > </property > <property name ="username" value ="${user}" > </property > <property name ="password" value ="${password}" > </property > </bean > <bean id ="sessionFactory" class ="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" > </property > <property name ="configLocations" value ="classpath:hibernate.cfg.xml" > </property > </bean > <bean id ="hibernateTemplate" class ="org.springframework.orm.hibernate4.HibernateTemplate" > <property name ="sessionFactory" ref ="sessionFactory" > </property > </bean > <bean id ="userDao" class ="com.course.dao.impl.UserDaoImpl" > <property name ="hibernateTemplate" ref ="hibernateTemplate" > </property > </bean > </beans >
3.application-service.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <bean id ="transactionManager" class ="org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name ="sessionFactory" ref ="sessionFactory" > </property > </bean > <tx:advice id ="advice" transaction-manager ="transactionManager" > <tx:attributes > <tx:method name ="add*" propagation ="REQUIRED" /> <tx:method name ="save*" propagation ="REQUIRED" /> <tx:method name ="insert*" propagation ="REQUIRED" /> <tx:method name ="update*" propagation ="REQUIRED" /> <tx:method name ="delete*" propagation ="REQUIRED" /> <tx:method name ="get*" propagation ="REQUIRED" /> <tx:method name ="query*" propagation ="REQUIRED" read-only ="true" /> <tx:method name ="*" propagation ="REQUIRED" read-only ="true" /> </tx:attributes > </tx:advice > <aop:config > <aop:pointcut id ="pc" expression ="execution(* com.course.service.impl.*.*(..))" /> <aop:advisor advice-ref ="advice" pointcut-ref ="pc" /> </aop:config > <bean id ="userService" class ="com.course.service.impl.UserServiceImpl" autowire ="byType" > </bean > </beans >
4.application-controller.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <bean id ="userController" class ="com.course.controller.UserController" autowire ="byType" > </bean > </beans >
5.applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <import resource ="classpath:application-dao.xml" /> <import resource ="classpath:application-service.xml" /> <import resource ="classpath:application-controller.xml" /> </beans >
补充: log4j.properties
1 2 3 4 5 6 7 8 log4j.rootLogger =INFO, stdout log4j.logger.org.mybatis.example.BlogMapper =TRACE log4j.appender.stdout =org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout =org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n