1. 导入Mybatis包
在github上下载jar包
https://github.com/mybatis/mybatis-3
打开上边的网页,然后拉倒最底下,点击链接进行下载。下载完解压后将mybatis.jar和lib下的所有jar包都进行导入。
Maven配置
https://github.com/mybatis/mybatis-3
里面有关于Mybatis的Maven配置
<dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>3.3.0</version><scope>compile</scope><optional>true</optional></dependency><dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.28.0-GA</version><scope>compile</scope><optional>true</optional></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.32</version><optional>true</optional></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version><optional>true</optional></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.17.0</version><optional>true</optional></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version><optional>true</optional></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.3.0</version><optional>true</optional></dependency>
</dependencies>
2. 配置Mybatis
SqlMapConfig.xml文件配置
SqlMapConfig.xml文件是Mbatis的主要配置文件,eclipse将文件放在src/main/java目录下,idea将文件放在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.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><!-- 加载POJO类的数据库操作文件 --><mappers><mapper resource="mapper/UserMapper.xml"/></mappers></configuration>
UserMapper.xml文件配置
UserMapper.xml里面编写了对User表的相应操作
<?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="userMapper"><!-- id类似于方法名,后边用来指定调用的 --><!-- resultType指定返回的结果类型,mybatis会自动根据变量名进行装配 --><select id="queryAll" resultType="model.User">select * from user</select><!-- parameterType指定传入sql语句的类型,可以为基本数据类型,也可以是封装类型例如Integer --><!-- 注意参数的取值要用花括号{} --><select id="queryById" parameterType="int" resultType="model.User">select * from user where id = #{id}</select><insert id="insert" parameterType="model.User">insert into user(id,name) values(#{id},#{name})</insert><!-- mybatis会根据参数名自动从对象中获取参数值 --><update id="update" parameterType="model.User">update user set name = #{name} where id = #{id}</update><delete id="delete" parameterType="java.lang.Integer">delete from user where id = #{id}</delete></mapper>
3. 测试
3.1 增
public void insert() {SqlSession session = null;try {String resource = "SqlMapConfig.xml";// 获取核心配置文件信息InputStream inputStream = Resources.getResourceAsStream(resource);// 获取session工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);inputStream.close();// 创建会话,不会自动提交事务session = sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}User user = new User();user.setId(1);user.setName("hello word");//通过namespace.id来进行查找方法session.insert("userMapper.insert", user);session.commit();if (session != null) {session.close();}}
3.2 删
public void delete() {SqlSession session = null;try {String resource = "SqlMapConfig.xml";// 获取核心配置文件信息InputStream inputStream = Resources.getResourceAsStream(resource);// 获取session工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);inputStream.close();// 创建会话,不会自动提交事务session = sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}session.delete("userMapper.delete", 1);session.commit();if (session != null) {session.close();}}
3.3 改
public void update() {SqlSession session = null;try {String resource = "SqlMapConfig.xml";// 获取核心配置文件信息InputStream inputStream = Resources.getResourceAsStream(resource);// 获取session工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);inputStream.close();// 创建会话,不会自动提交事务session = sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}User user = new User(10);user.setId(1);user.setName("hello mybatis");session.update("userMapper.update", user);session.commit();if (session != null) {session.close();}}
3.4 查
public void query() {SqlSession session = null;try {String resource = "SqlMapConfig.xml";// 获取核心配置文件信息InputStream inputStream = Resources.getResourceAsStream(resource);// 获取session工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);inputStream.close();// 创建会话,不会自动提交事务session = sqlSessionFactory.openSession();} catch (IOException e) {e.printStackTrace();}List<User> list = session.selectList("userMapper.queryAll");for(User u:list) {System.out.println("id:"+u.getId()+"name:"+u.getName());}if (session != null) {session.close();}}