第一步,在数据库中创建一个表,同时在代码中相应的添加上和这张表数据预一一对应的类 如下
Java语言:
Codee#21575
01
package
com
.
foreverlin
;
02 /*
03 * 数据表结构
04 */
05 public class Stu {
06
07 private int stuId ;
08 private String name ;
09 private String like ;
10
11 public int getStuId () {
12 return stuId ;
13 }
14 public void setStuId ( int stuId ) {
15 this . stuId = stuId ;
16 }
17 public String getName () {
18 return name ;
19 }
20 public void setName ( String name ) {
21 this . name = name ;
22 }
23 public String getLike () {
24 return like ;
25 }
26 public void setLike ( String like ) {
27 this . like = like ;
28 }
29 @Override
30 public String toString () {
31 return "Stu [stuId=" + stuId + ", name=" + name + ", like=" + like + "]" ;
32 }
33
34 }
02 /*
03 * 数据表结构
04 */
05 public class Stu {
06
07 private int stuId ;
08 private String name ;
09 private String like ;
10
11 public int getStuId () {
12 return stuId ;
13 }
14 public void setStuId ( int stuId ) {
15 this . stuId = stuId ;
16 }
17 public String getName () {
18 return name ;
19 }
20 public void setName ( String name ) {
21 this . name = name ;
22 }
23 public String getLike () {
24 return like ;
25 }
26 public void setLike ( String like ) {
27 this . like = like ;
28 }
29 @Override
30 public String toString () {
31 return "Stu [stuId=" + stuId + ", name=" + name + ", like=" + like + "]" ;
32 }
33
34 }
第二步,把相应的操作配上,面向接口编程哈
Java语言:
Codee#21576
01
package
com
.
foreverlin
;
02
03 import java.util.List ;
04
05 import org.springframework.stereotype.Repository ;
06 /*
07 * 数据表操作接口
08 */
09 @Repository
10 public interface StuDao {
11 public void addStu ( Stu stu );
12 public Stu getStuById ( Integer id );
13 public Integer deleteStuById ( Integer id );
14 public Integer updateStuById ( Stu stu );
15 public List < Stu > queryAllStu ();
16 }
02
03 import java.util.List ;
04
05 import org.springframework.stereotype.Repository ;
06 /*
07 * 数据表操作接口
08 */
09 @Repository
10 public interface StuDao {
11 public void addStu ( Stu stu );
12 public Stu getStuById ( Integer id );
13 public Integer deleteStuById ( Integer id );
14 public Integer updateStuById ( Stu stu );
15 public List < Stu > queryAllStu ();
16 }
Java语言:
Codee#21577
01
package
com
.
foreverlin
;
02
03 import java.util.List ;
04
05 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport ;
06 /*
07 * 数据表操作实现
08 */
09 public class StuDaoImpl extends SqlMapClientDaoSupport implements StuDao {
10
11 @Override
12 public void addStu ( Stu stu ) {
13 getSqlMapClientTemplate (). insert ( "StuDaoImpl.addStu" , stu );
14 }
15
16 @Override
17 public Stu getStuById ( Integer id ) {
18 return ( Stu ) getSqlMapClientTemplate (). queryForObject ( "StuDaoImpl.getStuById" , id );
19 }
20
21 @Override
22 public Integer deleteStuById ( Integer id ) {
23 return getSqlMapClientTemplate (). delete ( "StuDaoImpl.deleteStuById" , id );
24 }
25
26 @Override
27 public Integer updateStuById ( Stu stu ) {
28 return getSqlMapClientTemplate (). update ( "StuDaoImpl.updateStuById" , stu );
29 }
30
31 @Override
32 @SuppressWarnings ( "unchecked" )
33 public List < Stu > queryAllStu () {
34 return getSqlMapClientTemplate (). queryForList ( "StuDaoImpl.queryAllStu" );
35 }
36
37 }
02
03 import java.util.List ;
04
05 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport ;
06 /*
07 * 数据表操作实现
08 */
09 public class StuDaoImpl extends SqlMapClientDaoSupport implements StuDao {
10
11 @Override
12 public void addStu ( Stu stu ) {
13 getSqlMapClientTemplate (). insert ( "StuDaoImpl.addStu" , stu );
14 }
15
16 @Override
17 public Stu getStuById ( Integer id ) {
18 return ( Stu ) getSqlMapClientTemplate (). queryForObject ( "StuDaoImpl.getStuById" , id );
19 }
20
21 @Override
22 public Integer deleteStuById ( Integer id ) {
23 return getSqlMapClientTemplate (). delete ( "StuDaoImpl.deleteStuById" , id );
24 }
25
26 @Override
27 public Integer updateStuById ( Stu stu ) {
28 return getSqlMapClientTemplate (). update ( "StuDaoImpl.updateStuById" , stu );
29 }
30
31 @Override
32 @SuppressWarnings ( "unchecked" )
33 public List < Stu > queryAllStu () {
34 return getSqlMapClientTemplate (). queryForList ( "StuDaoImpl.queryAllStu" );
35 }
36
37 }
首先我们来配置spring的applicationConfig.xml文件,这里要添加入dataSource,transactionManager,sqlMapClient以及相应的操作对象的bean配置
各自的作用分别为数据源(你数据库的基本信息) 事物管理 ,存储指定的映射文件,是操作数据库的接口,其实主要就是配置上sqlMapConfig的文件路径。
XML语言:
Codee#21578
01
<?xml version="1.0" encoding="UTF-8"?>
02 <beans default-autowire= "byName"
03 xmlns= "http://www.springframework.org/schema/beans"
04 xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
05 xmlns:p= "http://www.springframework.org/schema/p"
06 xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
07
08 <bean id= "dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" >
09 <property name= "driverClassName" value= "com.mysql.jdbc.Driver" />
10 <property name= "url" value= "jdbc:mysql://localhost:3306/test" />
11 <property name= "username" value= "root" />
12 <property name= "password" value= "1" />
13 </bean>
14
15 <!-- 配置 -->
16 <bean id= "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
17 <property name= "dataSource" ref= "dataSource" />
18 </bean>
19
20
21 <bean id= "sqlMapClient" class= "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
22 <property name= "configLocation" value= "com/foreverlin/SqlMapConfig.xml" />
23 <property name= "dataSource" ref= "dataSource" />
24 </bean>
25
26
27 <!-- dao -->
28 <bean id= "stuDao" class= "com.foreverlin.StuDaoImpl" >
29 <property name= "sqlMapClient" ref= "sqlMapClient" />
30 </bean>
31 </beans>
02 <beans default-autowire= "byName"
03 xmlns= "http://www.springframework.org/schema/beans"
04 xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
05 xmlns:p= "http://www.springframework.org/schema/p"
06 xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
07
08 <bean id= "dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" >
09 <property name= "driverClassName" value= "com.mysql.jdbc.Driver" />
10 <property name= "url" value= "jdbc:mysql://localhost:3306/test" />
11 <property name= "username" value= "root" />
12 <property name= "password" value= "1" />
13 </bean>
14
15 <!-- 配置 -->
16 <bean id= "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
17 <property name= "dataSource" ref= "dataSource" />
18 </bean>
19
20
21 <bean id= "sqlMapClient" class= "org.springframework.orm.ibatis.SqlMapClientFactoryBean" >
22 <property name= "configLocation" value= "com/foreverlin/SqlMapConfig.xml" />
23 <property name= "dataSource" ref= "dataSource" />
24 </bean>
25
26
27 <!-- dao -->
28 <bean id= "stuDao" class= "com.foreverlin.StuDaoImpl" >
29 <property name= "sqlMapClient" ref= "sqlMapClient" />
30 </bean>
31 </beans>
sqlMapConfig.xml其实主要就一句话,告诉编译器我的ibatis操作映射文件在哪里
XML语言:
Codee#21579
1
<?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
3 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
4 <sqlMapConfig>
5
6 <sqlMap resource= "com/foreverlin/stu.xml" />
7 </sqlMapConfig>
2 <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
3 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
4 <sqlMapConfig>
5
6 <sqlMap resource= "com/foreverlin/stu.xml" />
7 </sqlMapConfig>
ibatis操作映射文件stu.xml
XML语言:
Codee#21580
01
<?xml version="1.0" encoding="UTF-8"?>
02 <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
03
04 <sqlMap namespace= "Stu" >
05
06 <typeAlias alias= "Stu" type= "com.foreverlin.Stu" />
07
08 <resultMap id= "Stu" class= "com.foreverlin.Stu" >
09 <result property= "stuId" column= "STU_ID" />
10 <result property= "name" column= "STU_NAME" />
11 <result property= "like" column= "STU_LIKE" />
12 </resultMap>
13
14 <select id= "StuDaoImpl.queryAllStu" resultMap= "Stu" >
15 SELECT * FROM STU
16 </select>
17
18 <insert id= "StuDaoImpl.addStu" parameterClass= "com.foreverlin.Stu" >
19 INSERT INTO STU (STU_ID,STU_NAME,STU_LIKE)
20 VALUES(#stuId#,#name#,#like#)
21 </insert>
22
23 <delete id= "StuDaoImpl.deleteStuById" >
24 DELETE FROM STU WHERE STU_ID = #value#
25 </delete>
26
27 <update id= "StuDaoImpl.updateStuById" parameterClass= "com.foreverlin.Stu" >
28 UPDATE STU
29 SET STU_ID = #stuId#, STU_NAME = #name#, STU_LIKE = #like# WHERE STU_ID = #stuId#
30 </update>
31
32 </sqlMap>
02 <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
03
04 <sqlMap namespace= "Stu" >
05
06 <typeAlias alias= "Stu" type= "com.foreverlin.Stu" />
07
08 <resultMap id= "Stu" class= "com.foreverlin.Stu" >
09 <result property= "stuId" column= "STU_ID" />
10 <result property= "name" column= "STU_NAME" />
11 <result property= "like" column= "STU_LIKE" />
12 </resultMap>
13
14 <select id= "StuDaoImpl.queryAllStu" resultMap= "Stu" >
15 SELECT * FROM STU
16 </select>
17
18 <insert id= "StuDaoImpl.addStu" parameterClass= "com.foreverlin.Stu" >
19 INSERT INTO STU (STU_ID,STU_NAME,STU_LIKE)
20 VALUES(#stuId#,#name#,#like#)
21 </insert>
22
23 <delete id= "StuDaoImpl.deleteStuById" >
24 DELETE FROM STU WHERE STU_ID = #value#
25 </delete>
26
27 <update id= "StuDaoImpl.updateStuById" parameterClass= "com.foreverlin.Stu" >
28 UPDATE STU
29 SET STU_ID = #stuId#, STU_NAME = #name#, STU_LIKE = #like# WHERE STU_ID = #stuId#
30 </update>
31
32 </sqlMap>
parameterClass代表要传入的参数路径和名称,返回参数推荐用resultMap哦亲,它可以取出多,而resultClass只能取出单个!