首先添加依赖。
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
复制代码
这是当前目录结构。
在resources目录下建立配置文件 sqlMapConfig.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
复制代码
首先应该配置的就是 enviroment,包涵数据库的信息。
transaction和datasource的type,以后再说吧。反正先这么放着就行了。
其中value可以使用引用的方法来配置,property的配置可以写在配置文件里,也可以在标签里。 在resources下面新建config.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/fortest?useUnicode=true&characterEncoding=UTF-8
复制代码
在sqlMapperConfig.xml中,添加新的标签。
<properties resource="config.properties">
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
复制代码
在sql的配置文件中,标签是有顺序的,properties和environment是平级的,但是应该在environment之前。如果配置错位置了,idea会给出提示。我把drivere、url、username和password分开配置,只是为了证明可以用这两种方式来添加配置项。
建一个特别简单的表。
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
`deptno` int(10) NOT NULL AUTO_INCREMENT,
`dname` varchar(20) NOT NULL,
`location` varchar(30) DEFAULT '',
PRIMARY KEY (`deptno`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of dept
-- ----------------------------
INSERT INTO `dept` VALUES ('1', 'one', 'china');
INSERT INTO `dept` VALUES ('2', 'two', 'america');
INSERT INTO `dept` VALUES ('3', 'three', 'mexico');
INSERT INTO `dept` VALUES ('4', 'four', 'spain');
复制代码
根据这个特别简单的表,写出pojo。
package com.mybatis.pojo;
public class Dept {
private int deptno;
private String dname;
private String location;
getter和setter省略了
@Override
public String toString() {
return ""+deptno+" "+dname+" "+location;
}
}
复制代码
在pojo文件夹中建立mapper,DeptMapper(java接口)。 在resources下建立对应的目录结构,com.mybatis.mapper,在其中建立DeptMapper.xml,看的一个说明是这样编译之后可以Mapper的class文件和xml文件会在同一个目录下,会有什么作用的,但是我给忘了,反正todo吧这块,想起来再记。放不放在同一个目录结构下都能用,反正这么先放着吧。现在是这个样子的。
DeptMapper的初始化。<?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>
</mapper>
复制代码
####mapper 的处理 首先要添加namespace,也就是对应的接口。
<mapper namespace="com.mybatis.mapper.DeptMapper">
</mapper>
复制代码
在DeptMapper中添加方法。
Dept getDept(int deptno);
复制代码
在DeptMapper.xml建立对应的select标签。
<mapper namespace="com.mybatis.mapper.DeptMapper">
<select id="getDept" resultType="com.mybatis.pojo.Dept" parameterType="_int">
select * from dept where deptno=#{deptno}
</select>
</mapper>
复制代码
select 的id要和接口中的方法名所对应。
DeptMapper写好了之后,要在配置文件里注册一下。
<mappers>
<mapper resource="com/mybatis/mapper/DeptMapper.xml"/>
</mappers>
复制代码
下面是test方法,目录如图。
注意其中Resources类,是ibatis包下的。
import com.mybatis.mapper.DeptMapper;
import com.mybatis.pojo.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
public class DeptTest {
private static SqlSessionFactory sqlSessionFactory=null;
@BeforeClass
public static void setUpClass() {
try {
String resource = "sqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testGetDept() {
SqlSession session=sqlSessionFactory.openSession();
DeptMapper mapper = session.getMapper(DeptMapper.class);
Dept dept = mapper.getDept(1);
System.out.println(dept.toString());
}
}
复制代码
测试成功。
这个select标签中,用的resultType是com.mybatis.pojo.Dept 如果包结构更复杂的话,会变得很长,所以mybatis有别名功能,在sqlMapCong.xml中配置,也与environment是一个级别的标签。 <typeAliases>
<typeAlias type="com.mybatis.pojo.Dept" alias="Dept"/>
</typeAliases>
复制代码
然后把select中的resulttype改为Dept,测试。
<select id="getDept" resultType="Dept" parameterType="_int">
select * from dept where deptno=#{deptno}
</select>
复制代码
当pojo简单的情况下,这样设置resultType是可以的,但是复杂之后就不行了,需要使用resultMap。 在DeptMapper中添加新的标签。
<resultMap id="Dept_base_resultmap" type="com.mybatis.pojo.Dept">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="location" property="location"/>
</resultMap>
复制代码
注意id是指数据库的主键,column对应的是数据库中的列名,property对应的是pojo中的变量名,一定要一一对应,要不然就是出现错误。再把select中的resultType改为resultMap,这二者不能同时使用。
<select id="getDept" resultMap="Dept_base_resultmap" parameterType="_int">
select * from dept where deptno=#{deptno}
</select>
复制代码
测试。 DeptMapper.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.mybatis.mapper.DeptMapper">
<resultMap id="Dept_base_resultmap" type="com.mybatis.pojo.Dept">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="location" property="location"/>
</resultMap>
<select id="getDept" resultMap="Dept_base_resultmap" parameterType="_int">
select * from dept where deptno=#{deptno}
</select>
</mapper>
复制代码
sqlMapConfig.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>
<properties resource="config.properties">
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
<typeAliases>
<typeAlias type="com.mybatis.pojo.Dept" alias="Dept"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url"
value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/mapper/DeptMapper.xml"/>
</mappers>
</configuration>
复制代码