本文主要内容:Mybatis在IDEA上基本环境配置、使用Mybatis完成对数据库的CRUD操作
Mybatis在IDEA上基本环境配置
- 使用Maven来创建一个简单的Mybatis项目
在pom.xml中添加Mybatis框架所需要依赖的坐标(jar包)
<dependencies> <!--Mybatis只需要前两个--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies>
完成主要配置文件的编写(数据库连接和指定映射配置文件的位置[目的是为了不用自己实现数据库查询方法])
<?xml version="1.0" encoding="UTF-8"?> <!-- 对应图中的SqlMapConfig.xml --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- Mybatis的主配置文件 --> <configuration> <!-- 配置properties,来引入连接数据库所需要的对象,即图中的jdbcConfig.properties --> <properties resource="jdbcConfig.properties"> <!-- 配置信息与使用JDBC连接数据库所需要信息相同 --> <!-- resource用于指定配置文件的信息,按照类路径的写法来写,并且必须存在于类路径下 --> </properties> <!-- 配置环境 --> <environments default="mysql"> <!-- 配置mysql环境 --> <environment id="mysql"> <!-- 配置事务的类型 --> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源(连接池) --> <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> <!-- 指定映射配置文件的位置(映射配置文件指的是每个dao独立的配置文件) --> <mappers> <mapper resource="live/warlock/dao/UserDao.xml"/> <!-- 注意这里的文件结构最好和dao层结构相同 --> </mappers> </configuration>
完成指定映射配置文件的位置,即UserDao.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="live.warlock.dao.UserDao"> <!-- 填写需要映射的Dao --> </mapper>
使用Mybatis完成对数据库的CRUD操作(都是在UserDao.xml的完成sql语句书写)
查询操作,在UserDao.xml,添加查询语句即可
<!-- 配置查询所有 --> <!--id对应dao接口中定义的方法 resultType对应查询结果的对应实体类 --> <select id="findAll" resultType="live.warlock.domain.User">select * from user;</select>
增加操作,在UserDao.xml,添加增加语句即可
<!-- 保存用户 --> <!--id对应dao接口中定义的方法 parameterType对应实体类的属性(注意赋值的语法#{xxx}) --> <insert id="saveUser" parameterType="live.warlock.domain.User">insert into user(username, address, sex, birthday) values (#{username}, #{address}, #{sex}, #{birthday});</insert>
删除操作,在UserDao.xml,添加删除语句即可
<!-- 删除用户 --> <delete id="deleteUser" parameterType="int">delete from user where id = #{id};</delete>
更改操作,在UserDao.xml,添加更改语句即可
<!-- 更新用户 --> <update id="updateUser" parameterType="live.warlock.domain.User">update user set username = #{username}, address = #{address}, sex = #{sex}, birthday = #{birthday} where id = #{id};</update>
模糊查询
<!-- 根据名称模糊查询用户 --> <!-- 使用模糊查询时,在调用方法时需要传参("%xxxx%") --> <select id="findUserByName" parameterType="String" resultType="live.warlock.domain.User">select * from user where username like #{name};</select>
聚合运算
<!-- 获取用户的总记录条数 --> <select id="findTotal" resultType="int">select count(*) from user;</select>
具体入门实例
在UserDao.xml补充实例所需要的sql语句
<!-- 配置查询所有 --> <select id="findAll" resultType="live.warlock.domain.User">select * from user;</select>
- 使用Mybatis完成快速入门查询 <!-- 注意在除查询外操作时需要手动使用session.commit()提交事务 -->