最近刚开始学习ssm框架,学到MyBatis,跟着B站的视频做了一个项目,原视频网址 B站原视频教程点这里
文章目录
1.mybatis文档官网
Mybatis中文文档官网
MyBatis官网也有很详细的教程,把这个文档给搞懂也是很有必要的
2.创建一个数据库并填一些数据进去
3.搭建环境
创建maven项目,把原来maven项目里面的src删掉,
1.父工程 pom.xml(在xml里面尽量不要写中文注释)
2.导入依赖
我的依赖用的时idea自带的,你们也可以自己去官网下载
junit, mybatis, mysql-connector-java
<!--父工程-->
<groupId>org.example</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<!--导入依赖-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
<scope>test</scope>
</dependency>
</dependencies>
4.创建模块
new一个module,这样这个新的子项目里面不用再重复导包,直接延用父项目里面的配置
编写mybatis核心配置文件
在子项目src–>main–>resource里面新建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>
<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/day14?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="haorun666"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Mapper/UserMapper.xml"/>
</mappers>
</configuration>
- driver驱动:一般都是com.mysql.jdbc.driver
- url:点击右侧database ,+datasource–>musql,输入user和passwword,test连接,可能会有时区错误,将时区改成GMT。连接成功后点击Scheme,选择需要连接的数据库,apply,ok。连接成功后点击右侧上方的data source properties,自动出现url,复制到“ "中去。
- username,password略
- mappers是映射的xml文件地址,之前xml是在com.rust.mapper下面,但是总是出bug,就把xml放在resource下面的mapper文件里面
编写mybatis工具类
com.rust.Mapper/pojo/Utils
在utils中创建mybatisUtils.java
package com.rust.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.InputStream;
import java.io.IOException;
public class mybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//第一步,获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建一个能执行sql的对象,sqlsession中包含了所有能执行SQL命令的方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
编写代码
-
实体类
pojo下创建User类
package com.rust.user; public class user { private int id; private String username; private String pwd; public user() { } public user(int id, String username, String pwd) { this.id = id; this.username = username; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString(){ return "User{"+ "id="+id + ",username'"+username+'\''+ ",pwd='"+pwd+'\''+ '}'; } }
-
DAO接口
userdao interface
package com.rust.Mapper; import com.rust.user.user; import java.util.List; public interface UserDao { List<user> getUserList(); }
-
接口实现类
接口实现类由原来的UserDaoImpl转换为一个Mapper配置文件
<?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.rust.Mapper.UserDao"> <!--查询语句--> <select id="getUserList" resultType="com.rust.user.user"> select * from user </select> </mapper>
5.测试
junit测试
在test下绿色的包测试,测试的包要和上面开发的包一一对应
创建userdaotest,里面可以使用try catch finally的句式
package com.rust.Mapper;
import com.rust.Utils.mybatisUtils;
import com.rust.user.user;
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();
//第二步,执行sql语句
//方式一
// UserDao mapper = sqlSession.getMapper(UserDao.class);
// List<user> listUser = mapper.getUserList();
//方式二
List<user> userList = sqlSession.selectList("com.rust.Mapper.UserDao.getUserList");
for (user user:userList){
System.out.println(user);
}
//关闭sqlsession
sqlSession.close();
}
}
测试结果
可能遇到的问题
1.type interface com.rust.mapper is not know to the mapperregistry(配置文件没有注册)
在pom.xml的bulid中添加
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
2.the error may exit in ///usermapper.xml(.xml不存在)
可以直接将xml复制到target下
3.时区问题,sreverTimezone=GMT
4.方法名不对
5.返回类型不对
6.maven导出资源问题