今天开始学习MyBatis 。
MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程,换句话说就是补了JDBC的坑,让操作数据库变得更方便。
学习MyBatis要跟着官方文档走,先贴个链接
https://mybatis.org/mybatis-3/zh/getting-started.html
1、创建一个普通的maven工程
2、在pom.xml中配置相关的依赖(mysql、mybatis、Junit、log4j)
<?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>org.example</groupId> <artifactId>mybatis</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>demo01</module> <module>demo02</module> </modules> <packaging>pom</packaging> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>org.apache.isis.viewer</groupId> <artifactId>junit</artifactId> <version>0.2.0-incubating</version> </dependency> </dependencies> </project>
3、编写核心配置文件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> <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/mybatisdb?userSSl=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="demo/dao/UserMapper.xml"/> </mappers> </configuration>
4、根据官网的指向,我们开始配置mybatis的工具类
package demo.utils; 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; 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(); } }
5、创建数据库,然后根据数据库编写对应的实体类
package demo.pojo; public class User { private int id; private String username; private String birthday; private String sex; private String address; public User() { } public User(int id, String username, String birthday, String sex, String address) { this.id = id; this.username = username; this.birthday = birthday; this.sex = sex; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday='" + birthday + '\'' + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } }
6、编写mapper接口,定义一些操作数据库的方法
package demo.dao; import demo.pojo.User; import java.util.List; import java.util.Map; public interface UserMapper { //查询所有用户 List<User> getUserList(); //根据id查询用户 User getUserById(int id); //插入一个用户 int insertUser(User user); // int insertUser2(Map<String,Object> map); //根据id修改用户 int updateUser(User user); //根据删除一个用户 int deleteUser(int id); }
7、按照传统jdbc的写法,我们应该编写实现类继承这个接口,实现里面的方法了;但是mybatis中不需要如此,我们需要用一个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="demo.dao.UserMapper"> <!-- select all users--> <select id="getUserList" resultType="demo.pojo.User"> select * from user </select> </mapper>
namespace对应要操作的接口,id对应实现的方法,resultType为返回值类型
8、编写测试类
public class MyTest { @Test public void selectUser() { SqlSession session = MybatisUtils.getSession(); //方法一: //List<User> users = session.selectList("com.kuang.mapper.UserMapper.selectUser"); //方法二: UserMapper mapper = session.getMapper(UserMapper.class); List<User> users = mapper.selectUser(); for (User user: users){ System.out.println(user); } session.close(); } }
需要注意:xml文件最好都放到resources文件夹下
运行之后,查询到数据库中的数据,成功!