MyBatis连接MySql数据库及使用教程(XML配置法)
MyBatis连接MySql数据库及使用教程(XML配置法)还有一个注解开发法这里先不说
(推荐一个SSM框架不错的视频,B站狂神)
1.创建一个空白的maven项目
2.打开项目的pom.xml文件(将下面代码粘贴进去,不用修改)
<!--导入依赖-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--在build中配置resouces,来防止资源导出失败问题-->
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
3.创建一个名为mybatis-config空白的XML文件(在src/main/resources目录下创建)
需要修改:
url(url里面有一个test 这个是你要调用的那个数据库的名称)
username( mysql登录账号)
password(mysql登录密码)
<?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/test?"/>
<property name="username" value="root"/>
<property name="password" value="Zjh520.1314."/>
</dataSource>
</environment>
</environments>
</configuration>
4.创建MyBatisUtils工具类(在cn/utils 目录下创建)
先在src/main/java下创建一个包,取名叫cn,然后再cn下再创建三个包,名为 dao、pojo、utils (创建包是为了更好的区分代码,使得自己更容易理解,不会乱)
如图:
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(){
SqlSession sqlSession= sqlSessionFactory.openSession();
return sqlSessionFactory.openSession();
}
}
5.创建名为User的实体类(在cn/pojo 目录下创建)
首先我们打开mysql数据库,然后创一个名为test的数据库,再创建一张名为user的表,表里创建三个属性,id、name、password (id为主键) ,在表里写下几条数据留着等下查询使用!
//实体类
public class User {
private int id;
private String name;
private String password;
public User(){}
public User(int id,String name ,String password){
this.id=id;
this.name=name;
this.password=password;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
6.创建名为UserMapper的接口(在cn/dao 目录下创建
public interface UserMapper {
//获取全部用户
List<User> getUserList();
}
7.创建名为UserMapper的XML文件(在cn/dao 目录下创建,我们的sql语句都是在这个文件里面完成的,将这段代码粘贴进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">
<mapper namespace="cn.dao.UserMapper">
<!--id对应前面接口写的方法名-->
<!--resultType sql执行后返回值类型-->
<!--parameterType 参数类型-->
<select id="getUserList" resultType="cn.pojo.User" >
select * from test.user
</select>
</mapper>
注意:UserMapper.xml搞定之后,我们要去mybatis-config.xml里将以下代码粘贴进去(将UserMapper.xml的路径配置进mybatis-config.xml)
8.测试sql(在src/test下创建一个名为UserMapperTest测试类)
public class UserMapperTest {
@Test
public void testAll(){
//第一步 获取sqlsession对象
SqlSession sqlSession=MyBatisUtils.getSqlSession();
try {
//第二步 执行sql
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
List<User> userList=userMapper.getUserList();
for (User user:userList){
System.out.println("查询全部:"+user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//第三步 关闭sqlSession
sqlSession.close();
}
}
}
测试结果图:
本文地址:https://blog.csdn.net/qq_35502206/article/details/110210443