1、文件结构
一共涉及3个文件夹8个文件,心累…
注意config文件夹和lib文件夹需要设置为资源文件夹,不然找不到文件
2、依赖下载
放入lib文件夹
mybatis: https://github.com/mybatis/mybatis-3/releases
mysql-connector: https://dev.mysql.com/downloads/connector/j/
3、数据准备
demo的数据库下有一个names的数据表,字段类型和数据如下
mysql> desc names; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(10) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | UNI | NULL | | | age | int(10) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ mysql> select * from names; +----+--------+------+ | id | name | age | +----+--------+------+ | 2 | 大红 | 27 | | 3 | 大壮 | 24 | | 4 | 秀英 | 25 | +----+--------+------+
4、Person类编写
其实就是一个普通的java类
Person.class
public class Person { private Integer id; private String name; private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String toString(){ return "[Person] id: "+ id + " name: " + name + " age :"+ age; } }
5、配置映射器
方式一 文件配置映射器:
映射接口 PersonMapper.class
public interface PersonMapper { public Person getPersonById(Integer id); }
映射文件 PersonMapper.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"> <!-- 1.PersonMapper为命名空间,为映射接口的全类名 2.id为唯一标识,与PersonMapper接口中的对应方法名相同 3.resultType为查询后的返回类型 4.#{id}指从传递过来的参数中取出id值 --> <mapper namespace="PersonMapper"> <select id="getPersonById" resultType="Person"> select id,name, age from names where id = #{id} </select> </mapper>
方式二 注解配置映射器:
PersonMapperAnnoation.class
import org.apache.ibatis.annotations.Select; public interface PersonMapperAnnoation { @Select("select id, name, age from names where id = #{id}") public Person getPersonById(Integer id); }
这样看来,方式2更为简洁和清晰
6、数据库配置填入数据库的地址,账号,密码
配置映射器,可以使用两种方式resource 和 class
<?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> <environments default="development"> <environment id="development"> <!-- 配置事务管理器,采用的是JDBC的管理器方式 --> <transactionManager type="JDBC"/> <!-- POOLED代表采用MyBatis内部提供的连接池方式 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/demo"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 引入映射器 包名+文件名--> <mappers> <mapper resource="PersonMapper.xml"/> <mapper class="PersonMapperAnnoation"/> </mappers> </configuration>
7、代码测试
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class mybatisDemo { public static void main(String[] args) throws IOException{ //1.根据MyBatis的配置文件,即mybatis-config.xml创建SqlSessionFactory SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); //2.获取session实例开启会话,其能直接执行*已经映射的SQL语句* SqlSession session = sqlSessionFactory.openSession(); try { // 方式1 查询 //3.获取接口的实现类对象 PersonMapper personMapper = session.getMapper(PersonMapper.class); //4.执行查询操作 Person person = personMapper.getPersonById(2); System.out.println(person); // 方式2 查询 //3.获取接口的实现类对象 PersonMapperAnnoation personMapperAnnoation = session.getMapper(PersonMapperAnnoation.class); //4.执行查询操作 Person person1 = personMapperAnnoation.getPersonById(2); System.out.println(person1); } finally { //4.关闭会话session session.close(); } } //根据MyBatis的配置文件,即mybatis-config.xml创建SqlSessionFactory public static SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory; } } /* 两种方式都能查到数据 [Person] id: 2 name: 大红 age :27 [Person] id: 2 name: 大红 age :27 */
参考