Mybatis的基本使用。
1、在mysql数据库中创建测试的数据库mybatis和测试数据表。
create database mybatis;
create table tb_country(
id int not null auto_increment,
country_name varchar(255) default null,
country_code varchar(255) default null,
primary key(id)
);
2、在数据表中插入测试数据。
insert into tb_country(country_name,country_code) values('中国','CN'),('美国','US'),('俄罗斯','RU'),('英国','GB'),('法国','FR');
3、pom.xml文件中引入mybatis和mysql的驱动jar包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency>
4、引入mybatis配置文件(告知mybatis如何工作)
<?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"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!--指定配置文件路径--> <mappers> <mapper resource="mapper/CountryDao.xml"/> </mappers> </configuration> 5、编写实体类Country package com.imooc.testmybatis; public class Country { private int id; private String countryName; private String countryCode; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } @Override public String toString() { return "Country{" + "id=" + id + ", countryName='" + countryName + '\'' + ", countryCode='" + countryCode + '\'' + '}'; } }
6、编写Dao接口
package com.imooc.dao; import com.imooc.testmybatis.Country; import java.util.List; public interface CountryDao { List<Country> selectAll(); }
7、编写mybatis核心文件(接口实现)
<?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.imooc.dao.CountryDao"> <select id="selectAll" resultType="com.imooc.testmybatis.Country"> SELECT id,country_name AS countryName,country_code AS countryCode FROM tb_country </select> </mapper
8、测试mybatis操作数据库
package com.imooc; import com.imooc.dao.CountryDao; import com.imooc.testmybatis.Country; 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 org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.io.Reader; import java.util.List; public class CountryMapperTest { private static SqlSessionFactory sqlSessionFactory; @BeforeClass public static void init(){ try { Reader reader = Resources.getResourceAsReader("config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } catch ( IOException e ) { e.printStackTrace(); } } @Test public void testSelectAll(){ SqlSession sqlSession = sqlSessionFactory.openSession(); try{ CountryDao countryMapper = sqlSession.getMapper(CountryDao.class); List<Country> countryList = countryMapper.selectAll(); System.out.println(countryList); }finally { sqlSession.close(); } } }
9、数据库返回数据
[
Country{id=1, countryName='中国', countryCode='CN'},
Country{id=2, countryName='美国', countryCode='US'},
Country{id=3, countryName='俄罗斯', countryCode='RU'},
Country{id=4, countryName='英国', countryCode='GB'},
Country{id=5, countryName='法国', countryCode='FR'}
]