2、第一个Mybatis程序
思路:搭建环境–>导入Mybatis–>编写代码–>测试!
2.1、搭建环境
1.搭建数据库
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')
2.新建项目
1.新建一个普通的maven项目
注意检查maven和maven仓库的路径 2.删除src目录,将此maven工程作为一个父工程
3.导入maven依赖
<groupId>org.example</groupId>
<artifactId>Mybatis-wd-study</artifactId>
<version>1.0-SNAPSHOT</version>
<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.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
2.2、创建一个模块
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="wd123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/lding/dao/UserMapper.xml"/>
</mappers>
</configuration>
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();
}
}
2.3、编写代码
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.lding.dao.UserDao">
<select id="getUserList" resultType="com.lding.pojo.User">
select * from mybatis.user
</select>
</mapper>
.4、测试
maven资源大于配置
maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被导出或者生效的问题, 解决方案:
父模块 和子模块的pom.xml都要加上
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include> </includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include> </includes>
</resource>
</resources>
</build>
junit测试
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();
}
}
思路整理
- 写好pojo实体类
- 写好工具类中的Class -MybatisUtils 加载sqlSessionFactory
- 工具类加载sqlSessionFactory 需要写mybatis-config.xml
- 写好mybatis-config.xml 主要实现连接数据库
- 创建Dao层的查询用户信息的接口
- 编写UserMapper.xml 用来实现Dao查询用户信息接口的功能
- 编写测试类 通过sqlSession.getMapper 得到mapper对象,通过maper对象调用getUserList方法
如果对您有帮助,免费的赞点一个 感谢🙏~~~
|