参考 http://www.cnblogs.com/wushiqi54719880/archive/2011/07/27/2117944.html
1、创建一个model对象
package adtec.dbtest.model; public class K_user { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
2、新建一个dao对象,对数据库的操作
package adtec.dbtest.dao; import adtec.dbtest.model.K_user; public interface K_userDao { public void insert(K_user k_user); }
3、创建与dao对象对应的xml操作文件(主要是执行它来操作数据库)
<?x ml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="adtec.dbtest.dao.K_userDao"> <insert id="insert" parameterType="adtec.dbtest.model.K_user"> INSERT INTO K_USER(NAME,AGE) values(#{name},#{age}) </insert> </mapper>
备注:xml文件名称要与dao文件名称对应,例如K_userDao对应的就是K_userDao.xml文件,id名称要与dao 的方法对应起来。
编写mybatis的配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="adtec/dbtest/dao/K_userDao.xml"/> </mappers> </configuration>
4、编写一个service接口,定义方法
package adtec.dbtest.service; import adtec.dbtest.model.K_user; public interface K_userService { public void insert(K_user k_user); }
5、对service接口的实现,主要目的是为了处理多个功能,方便事务的处理
package adtec.dbtest.service.imp; import adtec.dbtest.dao.K_userDao; import adtec.dbtest.model.K_user; import adtec.dbtest.service.K_userService; public class K_userServiceImp implements K_userService{ K_userDao k_userDao; public K_userDao getK_userDao() { return k_userDao; } public void setK_userDao(K_userDao k_userDao) { this.k_userDao = k_userDao; } @Override public void insert(K_user k_user) { k_userDao.insert(k_user); } }
6、spring配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://127.0.0.1:3306/hb</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <!-- 如果不是在src目录下面,而是在WEB-INF/config/mybatis目录下面 <property name="configLocation" value="classpath:../config/mybatis/MyBatis-Configuration.xml"></property> --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property> </bean> <bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="tm" /> <bean class="adtec.dbtest.service.imp.K_userServiceImp" name="k_userservice"> <property name="k_userDao" ref="k_userDao"></property> </bean> <!-- 配置dao层的接口,将接口提供给servivc使用 --> <bean id="k_userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="adtec.dbtest.dao.K_userDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
7、编写一个测试类
package adtec.dbtest.junit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import adtec.dbtest.model.K_user; import adtec.dbtest.service.K_userService; public class Test { @org.junit.Test public void insert() { ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml"); K_userService us = (K_userService) ap.getBean("k_userservice"); K_user u = new K_user(); u.setName("555"); u.setAge(555); us.insert(u); // Integer.parseInt("sss"); } }