思路:搭建环境—>导入Mybatis—>编写代码—>测试 1.搭建环境: 创建数据库“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')
新建项目: 1.新建一个普通的maven项目 2.删除src目录 3.导入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://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父工程-->
<groupId>com.yz</groupId>
<artifactId>mybatis-study</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入依赖-->
<dependency>
<!--mysql驱动-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
----------java基础搭建完事----------
创建一个模块: 新建:mybatis-config.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核心配置文件-->
<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=true&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="5201314"/>
</dataSource>
</environment>
</environments>
</configuration>
编写mybatis工具类
MybatisUtils.java
package com.yz.utils;
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 MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
编写代码: 实体类
User.java
package com.yz.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
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 + '\'' +
'}';
}
}
Dao接口 User.java
package com.yz.dao;
import com.yz.pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
接口实现类由原来的UerDaoImpl转变为一个Mapper配置文件
UserMapper.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">
<!--namespace==绑定一个对应的Dao/mapper接口-->
<mapper namespace="com.yz.dao.UserDao">
<!--查询语句-->
<select id="getUserList" resultType="com.yz.pojo.User">
select *from mybatis.user;
</select>
</mapper>
测试: 注意点: org.apache.ibatis.binding.BindingException: Type interface com.yz.dao.UserDao is not known to the MapperRegistry. 绑定异常,没有在mybatis配置文件中注册 MapperRegistry是什么? 核心配置文件中注册mappers
java.lang.ExceptionInInitializerError Caused by: org.apache.ibatis.exceptions.PersistenceException:
Error building SqlSession.
The error may exist in com/yz/dao/UserMapper.xml
解决方法: 在build中配置resources,来防止我们资源导出失败的问题
<!-- 在build中配置resources , 来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>***.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>***.xml</include>
</includes>
</resource>
</resources>
</build>
junit测试:
package com.yz.dao;
import com.yz.pojo.User;
import com.yz.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 = MybatisUtils.getSqlSession();
UserDao userdao = sqlSession.getMapper(UserDao.class);
List<User> userList = userdao.getUserList();
for (User user:userList) {
System.out.println(user);
}
sqlSession.close();
}
}
这个路项目经常出现的问题: 1.配置我呢见没有注册 2.绑定接口出错 3.方法名不对 4.返回类型不对 5.Maven导出资源问题
|