简单使用mybatis查询数据
1.jar包依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
2.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="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/><!-- 单独使用时配置成MANAGED没有事务 -->
<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>
<mapper resource="BlogMapper.xml"/>
</mappers>
</configuration>
3.映射文件
<?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="org.example.BlogMapper">
<resultMap id="BaseResultMap" type="org.example.Blog">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectBlogById" resultMap="BaseResultMap" statementType="PREPARED" >
select * from blog where id = #{id}
</select>
</mapper>
4.实体类
package org.example;
import java.io.Serializable;
public class Blog implements Serializable{
Integer id;
String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Blog{" +
"id=" + id+
", name='" + name + '\'' +
'}';
}
}
5.创建表
CREATE TABLE `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `blog` (`id`, `name`) VALUES (1, '博客');
测试
@Test
public void test() throws IOException {
String resources = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Blog blog = (Blog) sqlSession.selectOne("org.example.BlogMapper.selectBlogById", 1);
System.out.println(blog);
} finally {
sqlSession.close();
}
}
查询结果
Blog{id=1, name='博客'}
换一种查询方式
Blog blog = (Blog) sqlSession.selectOne("org.example.BlogMapper.selectBlogById", 1);
'org.example.BlogMapper.selectBlogById':是BlogMapper.xml的namespace
'selectBlogById':是sql的id,两者拼接成一个唯一的值
MyBatis根据这个唯一值才能找到sql语句,但是这个参数这么长,很容易写错,一旦错一个字符,就无法操作数据库了,下面来一种简单的方式,定义一个接口,接口的全限定名是xml中的namespace,方法名是sql中的id,然后查询方式就变成了这个样子:
//接口
package org.example;
public interface BlogMapper {
public Blog selectBlogById(Integer id);
}
//测试
@Test
public void testSelect() throws IOException {
String resources = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resources);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlogById(1);
System.out.println(blog);
} finally {
session.close();
}
}