s2sh整合<三>
Person.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="test.s2sh.bean.Person" table="person">
- <id name="id" type="java.lang.Integer" column="id">
- <generator class="increment"></generator>
- </id>
- <property name="name" type="string" column="name"
- length="20"></property>
- <property name="age" type="java.lang.Integer" column="age"></property>
- </class>
- </hibernate-mapping>
(3)在spring配置文件applicationContext.xml配置hibernate的连接数据库信息及sessionFactory创建方式
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <!-- apache.dbcp连接池的配置 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/s2sh?useUnicode=true&characterEncoding=UTF-8"></property>
- <property name="username" value="root"></property>
- <property name="password" value=""></property>
- <!-- 最大活动连接数 -->
- <property name="maxActive" value="100"></property>
- <!-- 最大可空闲连接数 -->
- <property name="maxIdle" value="30"></property>
- <!-- 最大可等待连接数 -->
- <property name="maxWait" value="500"></property>
- <!-- 默认的提交方式(如果不需要事务可以设置成true,在实际应用中一般设置为false,默认为false) -->
- <property name="defaultAutoCommit" value="true"></property>
- </bean>
- <!-- 这里直接使用spring对hibernate3支持的sessionFactory -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!-- hibernate方言等相关配置 -->
- <property name="hibernateProperties">
- <props>
- <prop key="connection.useUnicode">true</prop>
- <prop key="connection.characterEncoding">utf-8</prop>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- </props>
- </property>
- <!-- hbm.xml的映射文件 -->
- <property name="mappingResources">
- <list>
- <value>test/s2sh/bean/Person.hbm.xml</value>
- </list>
- </property>
- </bean>
- </beans>
如果出现乱码问题,可以考虑如下解决方案
或者将url后面加入编码串,如下
- <property name="url" value="jdbc:mysql://localhost:3306/s2sh?useUnicode=true&characterEncoding=UTF-8"></property>
注意 : 这里我的数据库编码是utf-8
具体那种方案好用大家可以尝试一下,我基本上直接将两种方式都加上了。
(4)下面我们开始写DAO层,首先创建test.s2sh.dao包放置Dao类的接口,创建test.s2sh.dao.impl包放置Dao类的实现。
PersonDAO.java
- package test.s2sh.dao;
- import java.util.List;
- import test.s2sh.bean.Person;
- public interface PersonDAO {
- void savePerson(Person p);
- void removePerson(Person p);
- Person findPersonById(Integer id);
- List<Person> findAllPersons();
- void updatePerson(Person p);
- }
由于使用了spring,所以DAO的实现类我们可以继承HibernateDaoSupport进行实现
PersonDAOImpl.java
- package test.s2sh.dao.impl;
- import java.util.List;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import test.s2sh.bean.Person;
- import test.s2sh.dao.PersonDAO;
- public class PersonDAOImpl extends HibernateDaoSupport implements PersonDAO {
- public List<Person> findAllPersons() {
- String hql = "from Person p order by p.id desc";
- return (List<Person>)this.getHibernateTemplate().find(hql);
- }
- public Person findPersonById(Integer id) {
- Person p = (Person) this.getHibernateTemplate().get(Person.class, id);
- return p;
- }
- public void removePerson(Person p) {
- this.getHibernateTemplate().delete(p);
- }
- public void savePerson(Person p) {
- this.getHibernateTemplate().save(p);
- }
- public void updatePerson(Person p) {
- this.getHibernateTemplate().update(p);
- }
- }
(5)下面我们开始进行service层的设计,这里的service层仅是简单调用DAO层的的方法。
同样是创建接口包(test.s2sh.service),实现包和接口类(test.s2sh.service.impl),实现类
PersonService.java
- package test.s2sh.service;
- import java.util.List;
- import test.s2sh.bean.Person;
- public interface PersonService {
- List<Person> findAll();
- void save(Person p);
- void delete(Person p);
- Person findById(Integer id);
- void update(Person p);
- }