简介:主要导入外部配置文件(jdbc.properties)
<properties resource="jdbc.properties"/>
<environments default="mysql">
<!-- 环境配置,即连接的数据库。 -->
<environment id="mysql">
<!-- 指定事务管理类型,type="JDBC"指直接简单使用
了JDBC的提交和回滚设置 -->
<transactionManager type="JDBC"/>
<!-- dataSource指数据源配置,POOLED是JDBC连接对
象的数据源连接池的实现。 -->
<dataSource type="POOLED">
<property name="driver"
value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username"
value="${jdbc.username}"/>
<property name="password"
value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
例如:com.gec.pojo.User用user来命名,简写了名称
<typeAliases>
<typeAlias type="com.gec.pojo.User" alias="user" />
</typeAliases>
例如有些自定义的数据类型,自己创建的user类
pojo数据类型---------->TypeHandler------->#{属性名}
#{属性名}--------->TypeHandler-------->pojo数据类型
创建一个数据表
create TABLE tb_user
(
ID int(11) primary key auto_increment,
NAME varchar(18) default null,
SEX char(2) default null,
AGE int(11) default null,
address varchar(20) default null
);
编写两个pojo
package com.gec.pojo;
import java.io.Serializable;
public class Address implements Serializable {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package com.gec.pojo;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private String sex;
private Integer age;
private Address address;
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
编写mapper文件
<?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.gec.mapper.UserMapper">
<!--编写定制sql
id:定制sql方法的id名
parameterType:输入类型
private String name;
private String sex;
private Integer age;
-->
<insert id="insert" parameterType="user">
<!--preparedStatement实现
insert into tb_user(NAME,SEX,AGE,address)
values (?,?,?,?)
preparedStatement.setString(columnIndex,strAddress);
-->
insert into tb_user(NAME,SEX,AGE,address)
values (#{name},#{sex},#{age},#{address})
</insert>
<!--根据id,获取用户信息
resultType属性:返回的数据类型
-->
<select id="findUserById" parameterType="int"
resultType="user">
select * from tb_user where id=#{id}
</select>
</mapper>
自定义一个typeHandler
package com.gec.typehandler;
import com.gec.pojo.Address;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AddressTypeHandler extends
BaseTypeHandler<Address> {
//实现赋值处理
//将Address属性对象------》#{address}
@Override
public void setNonNullParameter(PreparedStatement
preparedStatement, int columnIndex, Address address,
JdbcType jdbcType) throws SQLException {
//广东省,广州市
String
strAddress=address.getProvince()+","+address.getCity()
;
preparedStatement.setString(columnIndex,strAddress);
}
/*
* 指明字段名,来获取数据值
* */
@Override
public Address getNullableResult(ResultSet
resultSet, String cloumnName) throws SQLException {
//广东省,广州市
String
strAddress=resultSet.getString(cloumnName);
String province=strAddress.split(",")[0];
String city=strAddress.split(",")[1];
Address address=new Address();
address.setProvince(province);
address.setCity(city);
return address;
}
/*
* 指明字段对应的索引值,来获取数据值
* */
@Override
public Address getNullableResult(ResultSet
resultSet, int cloumnIndex) throws SQLException {
//广东省,广州市
String
strAddress=resultSet.getString(cloumnIndex);
String province=strAddress.split(",")[0];
String city=strAddress.split(",")[1];
Address address=new Address();
address.setProvince(province);
address.setCity(city);
return address;
}
/*
* 针对存储过程,来获取数据值
* */
@Override
public Address getNullableResult(CallableStatement
callableStatement, int cloumnIndex) throws
SQLException {
//广东省,广州市
String
strAddress=callableStatement.getString(cloumnIndex);
String province=strAddress.split(",")[0];
String city=strAddress.split(",")[1];
Address address=new Address();
address.setProvince(province);
address.setCity(city);
return address;
}
}
注册typeHandler(在mybatis-config.xml)
<typeHandlers>
<typeHandler
handler="com.gec.typehandler.AddressTypeHandler" />
</typeHandlers>
测试操作,添加数据
public static void insert()
{
SqlSession sqlSession=
MyBatisSessionUtils.openSession();
User user=new User();
user.setName("小苍老师");
user.setAge(30);
user.setSex("女");
Address address=new Address();
address.setProvince("广东省");
address.setCity("广州市");
user.setAddress(address);
//参数一:定制sql方法的id
//参数二:输入的pojo对象
sqlSession.insert("com.gec.mapper.UserMapper.insert",
user);
//提交事务
sqlSession.commit();
sqlSession.close();
}
根据id获取,观察address的数据值
public static void findUserById()
{
SqlSession
sqlSession=MyBatisSessionUtils.openSession();
int id=1;
User
user=sqlSession.selectOne("com.gec.mapper.UserMappe
r.findUserById",id);
System.out.println(user+"
"+user.getAddress().getProvince()+"
"+user.getAddress().getCity());
}