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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hammer?useUnicode=true&characterEncoding=utf8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>sqlMapConfig.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="studentDAO" class="com.hammer.dao.StudentDAOImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="studentDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref local="studentDAO"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
2、sqlMapConfig.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> <!-- 配置ibatis相关参数 --> <settings useStatementNamespaces="true"/><!-- 是否启用SQL脚本配置中的namespace --> <sqlMap resource="com/hammer/sqlmap/student_SqlMap.xml" /> </sqlMapConfig>
3、student_SqlMap.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 namespace="student" > <resultMap id="abatorgenerated_StudentResult" class="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> <result column="id" property="id" jdbcType="VARCHAR" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="SMALLINT" /> <result column="address" property="address" jdbcType="VARCHAR" /> </resultMap> <select id="abatorgenerated_getAllUsers" resultMap="abatorgenerated_StudentResult"> select id, name, age, address from student </select> <select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> select id, name, age, address from student where id = #id:VARCHAR# </select> <select id="abatorgenerated_selectByName" resultMap="abatorgenerated_StudentResult" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> select id, name, age, address from student where name = #name:VARCHAR# </select> <delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> delete from student where id = #id:VARCHAR# </delete> <insert id="abatorgenerated_insert" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> insert into student (id, name, age, address) values (#id:VARCHAR#, #name:VARCHAR#, #age:SMALLINT#, #address:VARCHAR#) </insert> <update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> update student set name = #name:VARCHAR#, age = #age:SMALLINT#, address = #address:VARCHAR# where id = #id:VARCHAR# </update> <update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.hammer.model.Student" > <!-- WARNING - This element is automatically generated by Abator for iBATIS, do not modify. This element was generated on Mon Apr 14 18:43:27 CST 2014. --> update student <dynamic prepend="set" > <isNotNull prepend="," property="name" > name = #name:VARCHAR# </isNotNull> <isNotNull prepend="," property="age" > age = #age:SMALLINT# </isNotNull> <isNotNull prepend="," property="address" > address = #address:VARCHAR# </isNotNull> </dynamic> where id = #id:VARCHAR# </update> </sqlMap>
4、model.Student类
public class Student { public Student(){ } public Student(String id,String name,Integer age,String address){ this.id=id; this.name=name; this.age=age; this.address=address; } /** * This field was generated by Abator for iBATIS. * This field corresponds to the database column student.id * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ private String id; /** * This field was generated by Abator for iBATIS. * This field corresponds to the database column student.name * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ private String name; /** * This field was generated by Abator for iBATIS. * This field corresponds to the database column student.age * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ private Integer age; /** * This field was generated by Abator for iBATIS. * This field corresponds to the database column student.address * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ private String address; /** * This method was generated by Abator for iBATIS. * This method returns the value of the database column student.id * * @return the value of student.id * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public String getId() { return id; } /** * This method was generated by Abator for iBATIS. * This method sets the value of the database column student.id * * @param id the value for student.id * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public void setId(String id) { this.id = id; } /** * This method was generated by Abator for iBATIS. * This method returns the value of the database column student.name * * @return the value of student.name * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public String getName() { return name; } /** * This method was generated by Abator for iBATIS. * This method sets the value of the database column student.name * * @param name the value for student.name * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public void setName(String name) { this.name = name; } /** * This method was generated by Abator for iBATIS. * This method returns the value of the database column student.age * * @return the value of student.age * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public Integer getAge() { return age; } /** * This method was generated by Abator for iBATIS. * This method sets the value of the database column student.age * * @param age the value for student.age * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public void setAge(Integer age) { this.age = age; } /** * This method was generated by Abator for iBATIS. * This method returns the value of the database column student.address * * @return the value of student.address * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public String getAddress() { return address; } /** * This method was generated by Abator for iBATIS. * This method sets the value of the database column student.address * * @param address the value for student.address * * @abatorgenerated Mon Apr 14 18:43:27 CST 2014 */ public void setAddress(String address) { this.address = address; } }
5、StudentDAO接口定义
import com.hammer.model.Student; import java.util.List; public interface StudentDAO { public List getList(); public Student getByName(String name); public Student getById(String id); public void save(Student student); public void delete(String id); public void update(Student student); }
6、StudentDAOImpl具体实现调用SQL语句
import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.hammer.model.Student; public class StudentDAOImpl extends SqlMapClientDaoSupport implements StudentDAO { public void delete(String id) { Student key = new Student(); key.setId(id); getSqlMapClientTemplate().delete("student.abatorgenerated_deleteByPrimaryKey", key); } public Student getById(String id) { Student key = new Student(); key.setId(id); Student record = (Student) getSqlMapClientTemplate().queryForObject("student.abatorgenerated_selectByPrimaryKey", key); return record; } public Student getByName(String name) { Student student = new Student(); student.setName(name); Student record = (Student) getSqlMapClientTemplate().queryForObject("student.abatorgenerated_selectByName", student); return record; } public List getList() { List list = getSqlMapClientTemplate().queryForList("student.abatorgenerated_getAllUsers", null); return list; } public void save(Student student) { getSqlMapClientTemplate().insert("student.abatorgenerated_insert", student); } public void update(Student student) { getSqlMapClientTemplate().update("student.abatorgenerated_updateByPrimaryKey", student); } }
相关推荐
spring3 ibatis sturts2搭建的简单项目,只供学习用,里面只有一个简单的例子,框架本身可用 使用mysql数据库
Struts+Spring+Ibatis整合框架搭建配置文档
strut+ibatis+spring环境搭建,有详细步骤。
ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729
是不是好东西你们去鉴定,SpringMVC + Spring + ibatis 可以配置多数据源,这个Demo 扩展性极强,就看你们自己如何发挥。
springmvc+ibatis demo搭建实现CRUD操作简单 有静态页面话技术
spring ibatis struts2搭建例子,非常好的例子,可扩展性强直接可以运行,记得给我点赞。
spring+struts+ibatis框架搭建完整搭建,可自行修改。
本人初次经验,初步实现的SSI框架的功能,希望能帮到初学者!
Spring2.5、Struts2、Ibati,祝大家学习进步!
很好的spring、struts和ibatis框架搭建流程,详细而且富有图片解释、
ibatis+spring+cxf+mysql搭建webservice的服务端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755
springmvc ibatis 整合, 解决BLOB,CLOB等大字段的问题,内置查询...内部并没有实际的项目,只是一个搭建好的环境,方便较少大家时间, 并提供了一个DEMO ,紧为不了解ibatis,或者首次整合springmvc-ibatis的人使用!
SSI框架搭建的基本代码,适合于初学者以及用此框架进行java开发的人,直接可以应用
自己亲自用eclipse+spring+ibatis搭建的基本框架,含有所需的JAR包,下载后只需更改sql2005的连接字符串,即WEB-INF/db-context.xml中的配置,并更改ibatis的xml文件中的sql语句即可运行。
成熟的SSI框架,mvc模式开发,该包中有成行的框架
搭建spring mvc+spring+ibatis所需所有jar包、亲测可用!!