思路:搭建环境 --> 导入Mybatis --> 编写代码 --> 测试
- CREATE DATABASE `mybatis`;
-
- USE `mybatis`;
-
- CREATE TABLE `user`(
- `id` INT(20) NOT NULL PRIMARY KEY,
- `name` VARCHAR(30) DEFAULT NULL,
- `pwd` VARCHAR(30) DEFAULT NULL
- ) ENGINE=INNODB DEFAULT CHARSET=utf8;
-
- INSERT INTO `user`(`id`,`name`,`pwd`) VALUES
- (1,'小明','123456'),
- (2,'张三','123456'),
- (3,'李四','123456');
新建一个普通的maven项目
删除src目录(作为父工程)
在pom.xml文件下导入maven依赖
- <dependencies>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.47version>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.7version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
- dependencies>
编写mybatis的核心配置文件(mybatis-config.xml)
- "1.0" encoding="UTF-8" ?>
- 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="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url"
- value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
- dataSource>
- environment>
- environments>
-
-
- <mappers>
- <mapper resource="com/yuan/dao/UserMapper.xml"/>
- mappers>
- configuration>
在java.com.xxx.utils层下编写mybatis工具类
- 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;
-
- //sqlSessionFactory --> sqlSession
- public class MybatisUtils {
-
- private static SqlSessionFactory sqlSessionFactory;
-
- static {
- //获取sqlSessionFactory对象
- String resource = "mybatis-config.xml";
- try {
- InputStream inputStream = Resources.getResourceAsStream(resource);
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- //既然有了SqlSessionFactory,顾名思义,我们就可以从中获得SqlSession的实例了
- //SqlSession 完全包含了面向数据库执行SQL命令所需的所有方法
-
- public static SqlSession getSqlSession(){
- //SqlSession sqlSession = sqlSessionFactory.openSession();
- return sqlSessionFactory.openSession();
- }
-
- }
在java.com.xxx.pojo层下编写实体类(以User为例)
- public class User {
- private int id;
- private String name;
- private String pwd;
-
- public User(int id) {
- this.id = id;
- }
-
- public User(int id, String name, String pwd) {
- this.id = id;
- this.name = name;
- this.pwd = pwd;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPwd() {
- return pwd;
- }
-
- public void setPwd(String pwd) {
- this.pwd = pwd;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", pwd='" + pwd + '\'' +
- '}';
- }
- }
在 java.com.xxx.dao下编写UserMapper(即Dao接口)
- import java.util.List;
- import java.util.Map;
-
- public interface UserMapper {
- //获取全部用户
- List
getUserList(); - }
在 java.com.xxx.dao编写接口实现类UserMapper.xml(由原来的UserDaoImpl转为一个Mapper配置文件)
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.yuan.dao.UserMapper">
-
-
- <select id="getUserList" resultType="com.yuan.pojo.User">
- select id, name, pwd from `user`
- select>
- mapper>
修改pom.xml文件
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>Mybatis-StudyartifactId>
- <groupId>com.yuangroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>mybatis-01artifactId>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
-
-
- <build>
- <resources>
- <resource>
- <directory>src/main/resourcesdirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
- <filtering>falsefiltering>
- resource>
- <resource>
- <directory>src/main/javadirectory>
- <includes>
- <include>**/*.propertiesinclude>
- <include>**/*.xmlinclude>
- includes>
- <filtering>falsefiltering>
- resource>
- resources>
- build>
-
- project>
在test.java.com.xxx.dao下编写UserDaoTest
- import com.yuan.pojo.User;
- import com.yuan.utils.MybatisUtils;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.Test;
-
- import java.util.List;
-
-
- public class UserDaoTest {
- @Test
- public void test(){
- //第一步:获得SqlSession对象
- SqlSession sqlSession = MybatisUtils.getSqlSession();
-
- try{
- //方式一:getMapper
- UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
- List
userList = userMapper .getUserList(); -
- //方式二:
- // List
userList = sqlSession.selectList("com.yuan.dao.UserDao.getUserList"); -
- for(User user : userList){
- System.out.println(user);
- }
-
- }catch(Exception e){
- e.printStackTrace();
- }finally {
- //关闭SqlSession
- sqlSession.close();
- }
- }
- }