1.所需要的包
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestMybatis</groupId>
<artifactId>TestMybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置文件
mybatis.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="jdbc.properties"/>
<settings>
<!--开启日至设置-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--不同的使用场景配置不同的环境-->
<!--default:默认使用的配置-->
<environments default="development">
<!--配置环境的id,自定义,配合default使用-->
<environment id="online">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--${}:搭配外部配置文档使用,在resource下创建jdbc.properties文件-->
<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>
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--&与'&'等价-->
<property name="url" value="jdbc:mysql://localhost:3306/testmybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--绑定dao.xml文件,mytatis会扫描指定的文件,才能执行dao.xml文件中的业务操作-->
<mappers>
<!--mapper:扫描指定的某一个文件-->
<mapper resource="dao/StudentDao.xml"/>
<!--package:扫描指定包下的所有dao.xml文件-->
<package name="dao"></package>
</mappers>
</configuration>
jdbc.xml
jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/testmybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username = root
jdbc.password = root
StudentDao.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">
<!--约束文件-->
<!--指定关联的dao接口-->
<mapper namespace="dao.StudentDao">
<!--当java类中的属性与数据库中的字段不同时,使用resultMap进行映射-->
<resultMap id="studentMap" type="domain.ObjectStudent">
<!--主键使用id标签-->
<id column="sid" property="oid"></id>
<!--result 普通字段使用result标签-->
<result column="email" property="oemail"></result>
<result column="sname" property="oname"></result>
<result column="age" property="oage"></result>
</resultMap>
<!--sql代码片段,搭配<include>标签使用,减少代码使用量-->
<sql id="selectStudent">
select * from student
</sql>
<!--select标签-->
<!--parameterType标签:传入数据的格式-->
<select id="selectStudentBySid" resultMap="studentMap" parameterType="Integer">
-- include标签,搭配sql标签使用
<include refid="selectStudent"/>
-- where标签,搭配if使用,会去掉第一个or或and
<where>
<if test="age > 0">
or sid=#{oid}
</if>
</where>
</select>
<!--insert标签-->
<insert id="insertStudent">
insert into student(sname,email,age) value(#{sname},#{email},#{age})
</insert>
<!--resultType标签:sql语句执行后的返回值使用什么接受,在使用mybatis的自动代理时,id和resultType需要与dao接口中的方法一一对应-->
<select id="selectStudentAll" resultType="domain.Student">
select * from student
</select>
<select id="selectForeach" resultType="domain.Student">
select * from student where sid in
<if test="list != null">
-- foreach标签:搭配in使用,collection:传入的集合有list集合和array数组
-- open:座分隔符
-- close:右分隔符
-- item:集合中元素的名称
-- separator:元素分隔符
<foreach collection="list" open="(" close=")" item="item" separator=",">
#{item}
</foreach>
</if>
</select>
</mapper>
MyBatisUtil.java
public class mybatisUtil {
static SqlSessionFactory sqlSessionFactory;
static {
//配置文件地址
String config="mybatis.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession session = null;
if(sqlSessionFactory!=null){
//默认为false,当设置为true时,开启自动事务提交
session=sqlSessionFactory.openSession(true);
}
return session;
}
}
当使用mybatis操作数据库时,使用下面这种方式创建sqlSession
并使用getMapper开启mybatis的自动代理,然后直接使用mapper中对应的方法即可。
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);