最近在狂神说里看了mybatis,下面分享以下在idea里创建我的第一个mybatis程序
思路:搭建环境->导入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,'肖战','521'),
(2,'王一博','1314'),
(3,'成毅','99');
1.新建一个普通的maven项目
2.删除src目录(以后在这个模块里建模块,不用在重新搭建环境)
3.导入maven依赖-------将下面的依赖代码置于 pom.xml 文件中
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
还有关于配置文件无法导出或生效问题会出现,我们在接着上一步在pom.xml里添加下面代码
<!--在build中配置resource,来防止我们资源导出失败问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
在此模块的里边新建一个普通maven项目
如图建包,备用
编写mybatis的核心配置文件
在resources包里建mybatis-config.xml文件,把以下代码复制进去
<?xml version="1.0" encoding="UTF8" ?>
<!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/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="111"/>
</dataSource>
</environment>
</environments>
<!-- 每一个Mapper.xml都需要在mybatis核心配置文件中注册-->
<mappers>
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>
编写mybatis工具类
在utils包里建MybatisUtils
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 (Exception e) {
e.printStackTrace();
}
}
//既然有了sqlsessionfactory,顾名思义,我们就可以获得sqlsession的实列了
//sqlsession完全包含了面向数据数执行sql命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
实体类-------------在pojo包里建User
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;
}
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
Dao接口---------在dao包里建UserDao类
public interface UserDao {
List<User> getUserList();
}
接口实现类由原来的UserDaoImpl转变为一个Mapper配置文件
在dao包里建UserMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="dao.UserDao">
<!--select查询语句-->
<select id="getUserList" resultType="pojo.User">
<!--mybatis.user表示mybatis下的user表-->
select * from user;
</select>
</mapper>
在test包里的java里建UserDaoTest类
public class UserDaoTest {
@Test
public void test(){
//第一步:获得SqlSession对象
SqlSession sqlSession= MybatisUtils.getSqlSession();
try {
//方式一:getMapper
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
//方式二
// List userList=sqlSession.selectList("dao.UserDao.getUserList");
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
//关闭sqlsession
sqlSession.close();
}
}
}