(一)在Eclipse中创建Dynamic Web Project项目并导入Spring3.2和MyBatis3的Jar包,本项目截图如下:
(二)创建Model类
package com.model;
public class OwnerNumInfo {
private int id;
public String phoneNum;
public String ownerName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String toString() {
return "User[id=" + id + " , ownerName=" + ownerName + " , phoneNum=" + phoneNum + "]";
}
}
(三)创建DAO接口
package com.dao;
import com.model.OwnerNumInfo;
public interface OwnerNumInfoDao {
public OwnerNumInfo getOwnerNumInfoDao(OwnerNumInfo owner);
public boolean addOwnerNumInfo(OwnerNumInfo owner);
public boolean deleteOwnerNumInfo(int id);
public boolean updateOwnerNumInfo(OwnerNumInfo owner);
}
(四)创建SQL语句映射文件
com.dao包下创建OwnerNumInfoDao.xml文件:
<?xml 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="com.dao.OwnerNumInfoDao">
<select id="getOwnerNumInfoDao" parameterType="com.model.OwnerNumInfo"
resultType="com.model.OwnerNumInfo">
SELECT * FROM ownernuminfo WHERE ownerName=#{ownerName} AND phoneNum=#{phoneNum}
</select>
<insert id="addOwnerNumInfo" parameterType="com.model.OwnerNumInfo"
flushCache="true">
INSERT INTO ownernuminfo (id,ownerName,phoneNum) VALUES
(#{id},#{ownerName},#{phoneNum})
</insert>
<update id="updateOwnerNumInfo" parameterType="com.model.OwnerNumInfo">
UPDATE ownernuminfo SET ownerName=#{ownerName},phoneNum=#{phoneNum} WHERE id=#{id}
</update>
<delete id="deleteOwnerNumInfo" parameterType="int">
DELETE FROM ownernuminfo WHERE id=#{id}
</delete>
</mapper>
(四)编写MyBatis配置文件
src目录下创建mybatis-config.xml文件:
<?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="com/dao/OwnerNumInfoDao.xml"/>
</mappers>
</configuration>
(五)编写Spring配置文件
src目录下创建ApplicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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-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="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.gjt.mm.mysql.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/phonenumdb?useUnicode=true&characterEncoding=UTF-8</value>
<!--springmybaitis是我的数据库 -->
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>1234</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="ownerNumInfoDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dao.OwnerNumInfoDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
(六)编写测试类
src下创建test.java:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dao.OwnerNumInfoDao;
import com.model.OwnerNumInfo;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=null;
ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
OwnerNumInfoDao ownerNumInfoDao=(OwnerNumInfoDao) ctx.getBean("ownerNumInfoDao");
OwnerNumInfo user=new OwnerNumInfo();
//添加两条数据
user.setId(1);
user.setOwnerName("Jessica");
user.setPhoneNum("123");
ownerNumInfoDao.addOwnerNumInfo(user);
user.setId(2);
user.setOwnerName("Jessica2");
user.setPhoneNum("123");
ownerNumInfoDao.addOwnerNumInfo(user);
System.out.println("添加成功");
//查询数据
user.setOwnerName("Jessica");
user.setPhoneNum("123");
System.out.println(ownerNumInfoDao.getOwnerNumInfoDao(user).toString());
user.setOwnerName("Jessica2");
user.setPhoneNum("123");
System.out.println(ownerNumInfoDao.getOwnerNumInfoDao(user).toString());
//修改数据
user.setId(2);
user.setPhoneNum("802");
ownerNumInfoDao.updateOwnerNumInfo(user);
System.out.println("修改成功");
//删除数据
ownerNumInfoDao.deleteOwnerNumInfo(1);
System.out.println("删除成功");
}
}
(七)用到的Jar包
除了Spring3.2和Mybatis3之外,还需要用到mybatis-spring-1.2.1.jar