环境:
- mysql
- idea
- jdk
- maven
创建数据库
CREATE TABLE `t_wuzong`(
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
pom.xml文件中导入所需依赖:
<!--在builder中配置resources,防止资源导出失败的问题-->
<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>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
配置mybatis所需的核心配置文件mybatis-config.xml(resources包下)
<?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/t_wuzong?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/wu/dao/UserMapper.xml"/>
</mappers>
</configuration>
写一个创建sqlsession的工具类MybatisUtils
package com.wu.util;
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;
//sqlSessionFactoryBuilder--->sqlSessionFactory--->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis第一获取sqlSessionFactory对象
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
接下来我们需要创建实体类与数据库表对应,可以使用lombok插件简化我们的代码
首先在pom.xml导入lombok依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
创建实体类
package com.wu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
通过添加注解自动添加了我们所需方法
然后我们需要去编写接口UserMapper
package com.wu.dao;
import com.wu.pojo.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//Dao=Mapper查询全部用户
// @Select("select * from user") 这是注解的方式
List<User> getUserList();
}
在这个目录下编写映射文件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.wu.dao.UserMapper">
<!--select查询语句-->
<select id="getUserList" resultType="com.wu.pojo.User">
select * from mybatis.user
</select>
</mapper>
测试一下!
public class UserDaoTest {
@Test
public void tset(){
SqlSession sqlSession= MybatisUtils.getSqlSession();
try{
//方式一:getMapper 推荐使用
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
//方式二
//List<User> userList = sqlSession.selectList("com.wu.dao.UserDao.getUserList");
for (User user:userList){
System.out.println(user);
}
}catch (Exception e){
}finally {
sqlSession.close();
}
}
成功!
分析一波!
- 首先我们配置了核心配置文件:编写了连接数据库所需要的环境、映射了mapper让主配置文件可以找到我们接口对应的映射文件
- 编写了能够创建sqlSession的工具类让我们更方便得到sqlSession对象简化代码
- 编写实体类,属性与数据库的字段一一对应
- 编写dao接口这里也叫mapper
- 在接口同一包下编写对应的xml文件:指定namespace命名空间找到接口,通过id找到对应的接口方法
- 测试:通过我们编写的工具类创建sqlSession对象、使用sqlSession对象得到mapper,通过mapper调用方法得到结果集输出!
转载:https://blog.csdn.net/weixin_45583358/article/details/105552855
查看评论