这里只做简单介绍,更多内容请参考[(https://mybatis.org/mybatis-3/zh/logging.html)]
一:简介
是持久层框架,支持自定义SQL、存储过程以及高级映射。免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。
二:入门
配置文件,将mybatis_lib.rar文件进行路径配置。eclipse中粘贴好后右键选中Build Path——Add Build Path
三、日志配置
Log4j:专门用来帮助打印日志信息。
配置 Log4J 比较简单。假设需要记录这个映射器的日志:
package org.mybatis.example;
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
在应用的类路径中创建一个名为 log4j.properties 的文件,文件的具体内容:
#全局日志配置
log4j.rootLogger=ERROR, stdout
#MyBatis 日志配置
log4j.logger.org.mybatis.example.BlogMapper=TRACE
#控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
配置Mybatis,并对其封装,减小代码冗余度:
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory ;
static {
//把映射进行封装
String resource = "mybatis.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory session:会话(连接数据库后就建立了一次会话,有了会话可以操作数据库)
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
// 得到SqlSession,factory:工厂
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
连接数据库中已创建的表格,并获取其字段属性
<mapper namespace="student">
<!-- resultMap最终是要将结果映射到Student上
当实体类的属性名和表的字段名不一致的时候,必须要写这个映射 -->
<resultMap type="Student" id="studentMap">
<!-- 映射主键属性:如果有多个主键字段,则定义多个id -->
<!-- property:类的属性名 -->
<!-- column:表的字段名 -->
<id column="id" property="id"/>
<!-- result定义普通属性 -->
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="gender" property="gender"/>
<association property="banji" javaType="Banji">
<id property="id" column="banjiId" />
<result property="name" column="banjiName"/>
</association>
</resultMap>
<sql id="studentColumns">
`id`,`name`,`age`,`gender`,`banji_id`
</sql>
</mapper>
查询操作:
@Test
public void testSelectAll() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
List<Student> list = sqlSession.selectList("student.selectAll");
for (Student student : list) {
System.out.println(student);
}
}
查询所有操作SQL语句
<!-- 返回的是List<Student>,但是resultType直接写里面的类型就可以 -->
<select id="selectAll" resultType="Student">
SELECT <include refid="studentColumns"/> FROM `student`
</select>
删除操作
```java
@Test
public void testDeleteById() throws IOException {
// Setting autocommit to false on JDBC Connection
// MyBatis把JDBC的autocommit设置为false,
// 当执行delete的时候并没有真正提交到数据库,对于更新类的操作需要手动提交。
// 在JDBC里面默认不需要用户手动提交因为autocommit 默认就是true,执行executeUpdate
// 的时候直接修改了数据库
SqlSession sqlSession = MyBatisUtil.getSqlSession();
int count = sqlSession.delete("student.deleteById", 59);
System.out.println("count: " + count);
// 对于更新类的操作需要手动提交
sqlSession.commit();
sqlSession.close();
}
删除语句
<!-- 对于更新类的操作返回的是影响的函数,但是resultType不需要写 -->
<delete id="deleteById" parameterType="Integer" >
DELETE FROM `student` WHERE `id`=#{id}
</delete>
插入操作
@Test
public void testInsert() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
// Student student = new Student("张三1212", 23, "男", 1);
// int count = sqlSession.insert("student.insert", student);
// System.out.println("count: " + count);
sqlSession.commit();
sqlSession.close();
}
插入语句
<insert id="insert" parameterType="Student">
INSERT INTO `student`(`name`,`age`,`gender`,`banji_id`) VALUES(#{name},#{age},#{gender},#{banjiId})
</insert>
更新操作
@Test
public void testUpdate() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
Student student = sqlSession.selectOne("student.selectById", 66);
System.out.println(student);
student.setName("李四1212");
student.setAge(24);
int count = sqlSession.update("student.update", student);
System.out.println("count: " + count);
sqlSession.commit();
sqlSession.close();
}
更新语句
<update id="update" parameterType="Student">
UPDATE
`student`
SET
`name`=#{name},
`age`=#{age},
`gender`=#{gender},
`banji_id`=#{banjiId}
WHERE
`id`=#{id}
</update>
查询有多少条记录
@Test
public void testUpdate() throws IOException {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
Student student = sqlSession.selectOne("student.selectById", 66);
System.out.println(student);
student.setName("李四1212");
student.setAge(24);
int count = sqlSession.update("student.update", student);
System.out.println("count: " + count);
sqlSession.commit();
sqlSession.close();
}
查询记录语句
<select id="selectTotalCount" resultType="Integer">
SELECT count(*) FROM `student`
</select>