一、新建项目
二、导入相关jar包
三、新建一个配置文件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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<!-- <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean> -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@kuhaier1.imwork.net:1521:ORCL" />
<property name="username" value="clound" />
<property name="password" value="clound" />
</bean>
<!-- 2. mybatis 的 SqlSession 的工厂: SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="typeAliasesPackage" value="com.zscs.model" /> -->
<property name="mapperLocations" value="com/zscs/model/*.xml" />
</bean>
<!-- 3. mybatis 自动扫描加载 Sql 映射文件 : MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zscs.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
四、编写数据库表对应的实体类User
package com.zscs.model;
public class User {
private Long id;
private String name;
private String sex;
private String nf;
private String mouth;
private String grjj;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
public String getNf() {
return nf;
}
public void setNf(String nf) {
this.nf = nf == null ? null : nf.trim();
}
public String getMouth() {
return mouth;
}
public void setMouth(String mouth) {
this.mouth = mouth == null ? null : mouth.trim();
}
public String getGrjj() {
return grjj;
}
public void setGrjj(String grjj) {
this.grjj = grjj == null ? null : grjj.trim();
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", nf="
+ nf + ", mouth=" + mouth + ", grjj=" + grjj + "]";
}
}
六、编写UserMapper.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.zscs.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.zscs.model.User" >
<id column="ID" property="id" jdbcType="DECIMAL" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="SEX" property="sex" jdbcType="VARCHAR" />
<result column="NF" property="nf" jdbcType="VARCHAR" />
<result column="MOUTH" property="mouth" jdbcType="VARCHAR" />
<result column="GRJJ" property="grjj" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
ID, NAME, SEX, NF, MOUTH, GRJJ
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from T_USER
where ID = #{id,jdbcType=DECIMAL}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from T_USER
where ID = #{id,jdbcType=DECIMAL}
</delete>
<insert id="insert" parameterType="com.zscs.model.User" >
insert into T_USER (ID, NAME, SEX,
NF, MOUTH, GRJJ)
values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{nf,jdbcType=VARCHAR}, #{mouth,jdbcType=VARCHAR}, #{grjj,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.zscs.model.User" >
insert into T_USER
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="name != null" >
NAME,
</if>
<if test="sex != null" >
SEX,
</if>
<if test="nf != null" >
NF,
</if>
<if test="mouth != null" >
MOUTH,
</if>
<if test="grjj != null" >
GRJJ,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=DECIMAL},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="nf != null" >
#{nf,jdbcType=VARCHAR},
</if>
<if test="mouth != null" >
#{mouth,jdbcType=VARCHAR},
</if>
<if test="grjj != null" >
#{grjj,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zscs.model.User" >
update T_USER
<set >
<if test="name != null" >
NAME = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
SEX = #{sex,jdbcType=VARCHAR},
</if>
<if test="nf != null" >
NF = #{nf,jdbcType=VARCHAR},
</if>
<if test="mouth != null" >
MOUTH = #{mouth,jdbcType=VARCHAR},
</if>
<if test="grjj != null" >
GRJJ = #{grjj,jdbcType=VARCHAR},
</if>
</set>
where ID = #{id,jdbcType=DECIMAL}
</update>
<update id="updateByPrimaryKey" parameterType="com.zscs.model.User" >
update T_USER
set NAME = #{name,jdbcType=VARCHAR},
SEX = #{sex,jdbcType=VARCHAR},
NF = #{nf,jdbcType=VARCHAR},
MOUTH = #{mouth,jdbcType=VARCHAR},
GRJJ = #{grjj,jdbcType=VARCHAR}
where ID = #{id,jdbcType=DECIMAL}
</update>
</mapper>
七、编写UserMapper.java
package com.zscs.dao;
import com.zscs.model.User;
public interface UserMapper {
int deleteByPrimaryKey(Long id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
八、编写测试类
package com.zscs.mytest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zscs.dao.UserMapper;
import com.zscs.model.User;
@RunWith(SpringJUnit4ClassRunner.class)//使用Spring的测试框架
@ContextConfiguration("/applicationContext.xml") //加载配置
public class TestMyBatis {
@Autowired
private UserMapper userMapper;
@Test
public void findUser(){
User user=userMapper.selectByPrimaryKey(Long.parseLong("1"));
user.setName("霍建华");
int i=userMapper.updateByPrimaryKey(user);
System.out.println(i==1 ? "更新成功":"更新失败");
user=userMapper.selectByPrimaryKey(Long.parseLong("1"));
System.out.println(user);
}
}