源码下载:http://download.csdn.net/detail/u011518709/8195143
主要jar包:
配置文件:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 整合spring --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>${jdbc.driverClassName}</value> </property> <property name="url"> <value>${jdbc.url}</value> </property> <property name="username"> <value>${jdbc.username}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> <property name="maxActive" value="100"/> <property name="maxIdle" value="30"/> <property name="maxWait" value="1000"/> <property name="defaultAutoCommit" value="true"/> <property name="removeAbandoned" value="true"/> <property name="removeAbandonedTimeout" value="60"/> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="configLocation"> <value>/WEB-INF/sql-map-config.xml</value> </property> </bean> <bean id="userdao" class="com.dao.UserDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient"/> </property> </bean> <bean id="userservice" class="com.service.UserServiceImpl"> <property name="userdao"> <ref bean="userdao" /> </property> </bean> </beans>
数据库配置文件:jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/exe_test jdbc.username=root jdbc.password=root
sql-map-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="com/pojo/User.xml"/> </sqlMapConfig>
主要介绍下:hibernate和sqlMapClient 查询的底层方法的区别:
UserdaoImpl.java
package com.dao; import java.sql.SQLException; import java.util.List; import com.ibatis.sqlmap.client.SqlMapClient; import com.pojo.User; public class UserDaoImpl implements UserDao{ private SqlMapClient sqlMapClient; public void setSqlMapClient(SqlMapClient sqlMapClient) { this.sqlMapClient = sqlMapClient; } public User Login(User u) { User user = null; try { user = (User)sqlMapClient.queryForObject("login", u); } catch (SQLException e) { e.printStackTrace(); } return user; } public boolean isInsert(User user) { boolean b; try { sqlMapClient.insert("insert", user); b = true; } catch (SQLException e) { e.printStackTrace(); b = false; } return b ; } public boolean isDelete(int id){ User user = null; boolean b; try{ sqlMapClient.delete("delete",id); b = true; }catch(SQLException e){ e.printStackTrace(); b = false; } return b; } public boolean update(User user) { boolean b ; try { sqlMapClient.update("update", user); b = true; } catch (Exception e) { b = false; } return b; } public User queryById(int id ) { User user = null; try { user = (User) sqlMapClient.queryForObject("querybyid",id); } catch (SQLException e) { e.printStackTrace(); } return user; } public List<User> queryAll() { List list = null; User user = new User(); try { list = sqlMapClient.queryForList("queryAll",user); } catch (SQLException e) { e.printStackTrace(); } return list; } }
User.xml (增删改查的语句)
<?xml version="1.0" encoding="gb2312" standalone="no"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap> <typeAlias alias="User" type="com.pojo.User" /> <resultMap id="UserResult" class="User"> <result property="id" column="ID" /> <result property="username" column="USERNAME" /> <result property="password" column="PASSWORD" /> </resultMap> <select id="login" resultMap="UserResult" parameterClass="User"> select * from USER where USERNAME=#username# and PASSWORD=#password# </select> <insert id="insert" parameterClass="User"> insert into USER(ID,USERNAME,PASSWORD)values(#id#,#username#,#password#) </insert> <delete id="delete" parameterClass="int"> delete from USER where ID = #id# </delete> <update id="update" parameterClass="User"> update USER set USERNAME=#username#,PASSWORD=#password# where ID = #id# </update> <select id="querybyid" parameterClass="int" resultClass="User"> select * from USER where ID = #id# </select> <select id="queryAll" parameterClass="User" resultClass="User"> select * from USER </select> </sqlMap>
ibatis后台查询的学习总结参考:http://blog.csdn.net/estelle_belle/article/details/41448589