Mybatis
文件结构图:
安装:
使用Mybatis时,需要在pom.xml中配制:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
在pom中还可以配置resources
,能够扫描配置路径下的所有以.xml
,.properties
,.tld
文件:
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
在资源文件下,新建db.properties文件,配置环境:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db0602
jdbc.username=root
jdbc.password=111111
env_choosen=deve
在资源文件下,新建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>
</configuration>
注意:在这个配置文件中,所有的标签都有严格的顺序
在<configuration>
标签下:
配置加载环境的文件: <properties resource="db.properties"/>
环境配置:
<environments default="${env_choosen}">
<environment id="deve">
<transactionManager type="JDBC"></transactionManager>
<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>
同时,配置mappers,自动获取xml文件
方式1:
<mapper resource="com/lanou/mapper/StudentMapper.xml"></mapper>
方式2:自动读取mapper包下的所有xml文件
<package name="com.lanou.mapper"/>
mapper包下新建接口文件
在接口文件StudentMapper
下写具体的方法
如:
public interface StudentMapper {
//查询所有学生
List<Student> findAllStudent();
//如果参数有多个,使用@Param注解规定参数的别名
void insertStudent(@Param("sname") String name);
void insertStu(Student student);
void delStu(@Param("sid") Integer id);
void updateStu(Student student);
}
然后建立对应的xml文件:StudentMapper.xml
,在这个xml文件中写sql语句:
<?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.lanou.mapper.StudentMapper">
<!--Mybatisde 结果映射
当实体类的属性名和数据库表的列名不一致的情况下,
需要使用结果映射来确保数据的一致性
-->
<resultMap id="BaseMap" type="com.lanou.bean.Student">
<!--column:数据库表列名
property:实体类的属性名-->
<id column="id" property="id"/>
<result column="name" property="name"/>
</resultMap>
<!--保存一段sql片段,以供反复使用-->
<sql id="Column_List">
id,name
</sql>
<select id="findAllStudent" resultType="BaseMap">
SELECT
<include refid="Column_List"/>
FROM student
</select>
<insert id="insertStudent">
INSERT INTO student VALUES (null,#{sname})
</insert>
<insert id="insertStu" parameterType="com.lanou.bean.Student">
INSERT INTO student VALUES (NULL ,#{name})
</insert>
<delete id="delStu">
DELETE FROM student WHERE id=#{sid}
</delete>
<update id="updateStu">
UPDATE student SET name=#{name} WHERE id=#{id}
</update>
</mapper>
获取SQLSessionFactory
//1.加载config文件
String re = "config.xml";
InputStream stream = Resources.getResourceAsStream(re);
//2.创建SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
获取SQLSession对象
SqlSession session = factory.openSession();
getMapper
StudentMapper mapper = session.getMapper(StudentMapper.class);
然后通过 mapper调用方法
对于像 StudentMapper 这样的映射器类(Mapper class)来说,它们的映射的语句可以不需要用 XML 来做,取而代之的是可以使用 Java 注解。比如,上面的 XML 示例可被替换如下:
@Select("SELECT * FROM student WHERE id = #{id}")
Student findById(Student student);