1.导入相关的jar包
2.建立数据库
1 create table account(
2 id int(10), 3 user varchar(50), 4 paw varchar(50) 5 ); 6 insert into account values(1,'admin','admin');
3.建立包结构
4.配置文件的配置及代码
4.1 数据库配置文件:db.properties
1 #jdbc
2 jdbc.driver=com.mysql.jdbc.Driver
3 jdbc.url=jdbc:mysql://127.0.0.1:3306/test 4 jdbc.username=root 5 jdbc.password=
4.2 spring配置文件:applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 11 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 12 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 13 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 14 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 15 <context:component-scan base-package="ssh.ft"></context:component-scan> 16 17 <context:property-placeholder location="classpath:configs/db.properties" /> 18 <!-- datasource --> 19 <bean id="dataSource" 20 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 21 <property name="driverClassName" value="${jdbc.driver}" /> 22 <property name="url" value="${jdbc.url}" /> 23 <property name="username" value="${jdbc.username}" /> 24 <property name="password" value="${jdbc.password}" /> 25 </bean> 26 <!-- spring与hibernate整合 spring来管理session的创建、打开和关闭 --> 27 <bean id="sessionFactory" 28 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 29 <!-- 通过配置文件的方式获取数据源,出现异常,未解决 --> 30 <property name="hibernateProperties"> 31 <props> 32 <prop key="connection.useUnicode">true</prop> 33 <prop key="connection.characterEncoding">utf-8</prop> 34 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 35 <prop key="hibernate.show_sql">true</prop> 36 <prop key="hibernate.hbm2ddl.auto">update</prop> 37 </props> 38 </property> 39 <property name="dataSource" ref="dataSource" /> 40 <property name="mappingResources"> 41 <list> 42 <!-- 以下用来列出所有的PO映射文件 --> 43 <value>configs/account.hbm.xml</value> 44 </list> 45 </property> 46 </bean> 47 <!-- 定义事物管理器,并位事物管理器配置上述所定义的session --> 48 <!-- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 49 <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> 50 </bean> <tx:annotation-driven transaction-manager="transactionManager"/> --> 51 52