(一) - MyBatis初体验
用一个 Java Maven 项目说明 MyBatis 的基本使用
开始之前已准备好数据库和相关数据用户演示, 这里已创建好ryan数据库, student表, 表数据如下:
新建Maven项目 mybatis01:
一. 导入 mybatis: (以及mybatis依赖的mysql包)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ryan</groupId> <artifactId>mybatis01</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> </dependencies> </project>
二. 在resources目录下创建mybatis的配置文件 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> <!--配置MyBatis运行环境--> <environments default="development"> <environment id="development"> <!--配置JDBC的事务管理--> <transactionManager type="JDBC"></transactionManager> <!--POOLED配置JDBC数据源连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <!-- 如下, 如果不指定编码, 可能会报空指针异常--> <!-- <property name="url" value="jdbc:mysql://localhost:3306/ryan?serverTimezone=GMT%2B8"/>--> <property name="url" value="jdbc:mysql://localhost:3306/ryan?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="613025"/> </dataSource> </environment> </environments> <!--注册userMapper.xml--> <mappers> <mapper resource="com/ryan/mapper/userMapper.xml"></mapper> </mappers> </configuration>
三. 在新建的包下创建mapper配置文件, (每一个Java类都有一个对应的mapper, 在此配置文件中写sql):
<?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的值为mapper配置文件的路径, 不用写后缀--> <mapper namespace="com.ryan.mapper.userMapper"> <!--parameterType的值为对应的Java类, 之后将通过id调用对应sql--> <insert id="save" parameterType="com.ryan.javaClass.User"> </insert> </mapper>
四. 将mapper配置文件加载到mybais的配置文件 mybatis-config.xml 中:
... </environment> </environments> <!--注册userMapper.xml--> <mappers> <mapper resource="com/ryan/mapper/userMapper.xml"></mapper> </mappers> </configuration>
五. 创建User类, 其属性名和类型需与数据表中的字段相对应:
package com.ryan.javaClass; public class User { private int id; private String name; private int phoneNumber; public User(int id, String name, int phoneNumber) { this.id = id; this.name = name; this.phoneNumber = phoneNumber; } }
六. 创建Test类, 在这里操作数据库数据:
public class Test { public static void main(String[] args) { //加载MyBatis配置文件 InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //需要先获取SqlSessionFactoryBuilder, 通过其获取SqlSessionFactory, 再通过其获取SqlSession, 最后用SqlSession操作数据 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); String statement = "com.ryan.mapper.userMapper.save"; User user = new User(5488, "嬴政", 668688); sqlSession.insert(statement, user); sqlSession.commit(); } }
七. 编写sql:
<!--namespace的值为mapper配置文件的路径, 不用写后缀--> <mapper namespace="com.ryan.mapper.userMapper"> <!--parameterType的值为对应的Java类, 之后将通过id调用对应sql--> <insert id="save" parameterType="com.ryan.javaClass.User"> insert into student(id,name,phoneNumber)values(#{id},#{name},#{phoneNumber}) </insert> <insert id="search" parameterType="com.ryan.javaClass.User"> select * from student where id = 5877 </insert> </mapper>
八. 运行Test.main():
在运行前, 还需进行一些步骤, IDEA默认只能读取resources目录下的配置文件, 上面的mapper文件是在java目录下的, 所以对此我们需要进行一些配置让IDEA能读取到此配置文件, 方法是在pom中添加如下配置:
... <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> </dependencies> <!--IDEA默认只能读取resource目录下的配置文件, 此配置设置IDEA能读取设定目录下的所有配置文件--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
运行后刷新数据库: