下图为建好的大致图:
手动创建mybaties的过程如下:
1、maven引入相关的依赖:
<?xml version="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:mybaties//maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MyBatiesDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
//工具包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
//测试框架
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
//jdbc
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2、进行mybaties配置文件配置:
<?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="config.properties">
<property name="username" value="root"/>
<property name="password" value="19951230"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/BlogMapper.xml"/>
</mappers>
</configuration>
3、写mapper接口:
package mapper;
import dao.Blog;
public interface BlogMapper {
Blog selectBlog(Integer id);
}
4、写dao访问对象javabean:
package dao;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Blog {
private Integer id;
private String name;
}
5、写mapper.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">
<mapper namespace="mapper.BlogMapper">
<select id="selectBlog" resultType="dao.Blog">
select * from Blog where id = #{id}
</select>
</mapper>
6、数据库配置信息:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.0.107:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=19951230
7、测试类:
package com.kexuexiong;
import dao.Blog;
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 Test {
@org.junit.Test
public void test() throws IOException {
String resource="mybaties-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try(SqlSession session=sqlSessionFactory.openSession()){
Blog selectOne = (Blog)session.selectOne("mapper.BlogMapper.selectBlog", 101);
System.out.println(selectOne);
}catch (Exception e){
e.printStackTrace();
}
}}
8、数据库脚本:
/*
Navicat MySQL Data Transfer
Source Server : mysql
Source Server Version : 50564
Source Host : 192.168.0.107:3306
Source Database : shiro
Target Server Type : MYSQL
Target Server Version : 50564
File Encoding : 65001
Date: 2020-03-13 20:44:09
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for Blog
-- ----------------------------
DROP TABLE IF EXISTS `Blog`;
CREATE TABLE `Blog` (
`name` char(11) DEFAULT NULL,
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of Blog
-- ----------------------------
INSERT INTO `Blog` VALUES ('xiong', '1');
INSERT INTO `Blog` VALUES ('yi', '101');
9、点击运行:
结果如下:
Blog(id=101, name=yi)
Process finished with exit code 0