1、spring配置管理
applicationContext.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: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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 读取数据库信息资源文件jdbc.properties -->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置ibatis -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>classpath:sqlMapConfig.xml</value>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务特性配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="NESTED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 声明式事务aop -->
<aop:config>
<aop:pointcut id="fooServiceMethods" expression="execution(* com.cjf.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" />
</aop:config>
</beans>
2、jdbc.properties
###########
# MySQL 5 #
###########
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/dljd?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.max=10
3、sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/cjf/domain/user_sqlMap.xml" />
</sqlMapConfig>
4、entity
public class TUser {
private Integer id;
private String username;
private String sex;
private String age;
private String del;
private String bz;
private String bz1;
...
}
5、entity.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="user" type="com.cjf.domain.TUser"/>
<select id="loginAction" parameterClass="user" resultClass="int">
<![CDATA[
select count(*) from t_user where username=#username# and pwd=#pwd#
]]>
</select>
<select id="findByPage" parameterClass="int" resultClass="user">
<![CDATA[
select * from t_user where username not in('admin') order by id desc limit #pageNo#,5
]]>
</select>
<select id="getPages" resultClass="int">
<![CDATA[
select count(*) from t_user where username not in('admin')
]]>
</select>
<insert id="saveUser" parameterClass="user">
<![CDATA[
insert into t_user (username,sex,age) values (#username#,#sex#,#age#)
]]>
</insert>
<delete id="deleteUser" parameterClass="int">
<![CDATA[
delete from t_user where id=#id#
]]>
</delete>
<select id="findById" resultClass="user" parameterClass="int">
<![CDATA[
select * from t_user where id=#id#
]]>
</select>
<update id="updateUser" parameterClass="user">
<![CDATA[
update t_user set username=#username#,sex=#sex#,age=#age# where id=#id#
]]>
</update>
</sqlMap>
6、daoImpl.java
public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao {
public void saveUser(TUser user) {
getSqlMapClientTemplate().insert("saveUser", user);
}
public void deleteUser(int id) {
getSqlMapClientTemplate().delete("deleteUser", id);
}
public TUser findById(int id) {
return (TUser) getSqlMapClientTemplate().queryForObject("findById",id);
}
@SuppressWarnings("unchecked")
public List<TUser> findByPage(int pageNo) {
return getSqlMapClientTemplate().queryForList("findByPage",pageNo);
}
public int getPages() {
int rows = (Integer)getSqlMapClientTemplate().queryForObject("getPages");
if(rows%5==0)
return rows/5;
return rows/5+1;
}
public boolean login(TUser user) {
if(loginAction(user)>0)
return true;
return false;
}
private int loginAction(TUser user) {
return (Integer)getSqlMapClientTemplate().queryForObject("loginAction",user);
}
public void updateUser(TUser user) {
getSqlMapClientTemplate().update("updateUser", user);
}
}