创建mybatis项目
Idea创建empty project
项目名MyBatis
Project Struct
SDK选1.8
language level 8
maven setting为修改了中央仓库的
创建maven module
修改pom.xml
<?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>MyBaits_demo1</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
创建配置文件src/main/resources/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> <!-- properties?,settings?,typeAliases?,typeHandlers?,objectFactory?, objectWrapperFactory?,reflectorFactory?,plugins?,environments?, databaseIdProvider?,mappers? --> <properties resource="jdbc.properties"></properties> <!--设置类型别名 属性: type:设置需要设置别名的类型 alias:设置某个类型的别名,若不设置该属性,那么该类型拥有默认的别名,即类名且不区分大小写 --> <typeAliases> <typeAlias type="com.company.mybatis.pojo.User" alias="User"></typeAlias> <!--以包为单位,将包下所有的类型设置默认的类型别名,即雷名且不区分大小写--> <package name="com.company.mybatis.pojo"/> </typeAliases> <!--设置连接多个数据库的环境 属性 default: 默认使用的环境 --> <environments default="development"> <!-- 配置某个具体的环境 属性 id:表示连接数据库的环境唯一标示,不能重复 --> <environment id="development"> <!-- 设置事务管理方式 属性 type="JDBC|MANAGED" JDBC:表示当前环境中,执行SQL时,使用的时JDBC中原生的事务管理方式,事务的提交或回滚手动 MANAGED:被管理,例如Spring --> <transactionManager type="JDBC"/> <!-- 配置数据源 属性: type:设置数据源的类型 type="POOLED|UNPOOLED|JNDI" POOLED:表示使用数据库缓存数据库连接 UNPOOLED:表示不使用数据库连接池 JNDI:表示使用上下文中的数据源 --> <dataSource type="POOLED"> <!--设置连接数据库的驱动--> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> <!-- 配置某个具体的环境 属性 id:表示连接数据库的环境唯一标示,不能重复 --> <environment id="test"> <!-- 设置事务管理方式 属性 type="JDBC|MANAGED" JDBC:表示当前环境中,执行SQL时,使用的时JDBC中原生的事务管理方式,事务的提交或回滚手动 MANAGED:被管理,例如Spring --> <transactionManager type="JDBC"/> <!-- 配置数据源 属性: type:设置数据源的类型 type="POOLED|UNPOOLED|JNDI" POOLED:表示使用数据库连接池缓存数据库连接 UNPOOLED:表示不使用数据库连接池 JNDI:表示使用上下文中的数据源 --> <dataSource type="POOLED"> <!--设置连接数据库的驱动--> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!--引入映射文件--> <mappers> <!-- <mapper resource="mappers/UserMapper.xml"/>--> <!--以包为单位引入映射文件 要求: 1。mapper接口所在的包要和映射文件所在的包一致 2。mapper接口要和映射文件的名字一致 解决引入过多resource文件问题 resource下建立目录要用/,不要用. --> <package name="com.company.mybatis.mapper"/> </mappers> </configuration>
数据库配置信息写在src/main/resources/jdbc.properties
创建实体类,对应数据库表user
src/main/java/com/company/mybatis/pojo/User.java
public class User { private Integer user_id; private String username; private String ustatus; @Override public String toString() { return "User{" + "user_id=" + user_id + ", username='" + username + '\'' + ", ustaus='" + ustatus + '\'' + '}'; } public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUstatus() { return ustatus; } public void setUstatus(String ustatus) { this.ustatus = ustatus; } public User() { } }
创建mapper interface接口
src/main/java/com/company/mybatis/mapper/UserMapper.java
mapper类似于dao,区别在mapper仅仅是接口,不需要实现
public interface UserMapper { /* MyBaits面向接口编程 * 两个一致 * 1。映射文件的namespace要和mapper接口的全类名保持一致 * 2。映射文件中SQL语句的id要和mapper接口中的方法名一致 * 相当于原来的dao 表 -- 实体类 --mapper接口 --映射文件 * * 添加用户信息 * */ int insertUser(); /* 修改用户信息 * */ void updateUser(); List<User> getAllUser(); /* 删除用户信息 * */ void deleteUser(); User getUserById(); }
创建mybatis 的映射文件
src/main/resources/com/company/mybatis/mapper/UserMapper.xml
规则为实体类名+Mapper.xml
创建resources下的文件时,目录不能用.分割/com/company/mybatis/mapper/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="com.company.mybatis.mapper.UserMapper"> <!--int insertUser();--> <insert id="insertUser"> insert into t_user values(1234,'mybaits','123') </insert> <!--updateUser()--> <update id="updateUser"> update t_user set username="upmybaits" where user_id=1234 </update> <!--deleteUser()--> <delete id="deleteUser"> delete from t_user where user_id=1234 </delete> <!--getUserById()--> <select id="getUserById" resultType="com.company.mybatis.pojo.User"> select * from t_user where user_id=12345 </select> <!--getUserById()--> <select id="getAllUser" resultType="User"> select * from t_user </select> </mapper>
添加测试程序
src/test/java/com/company/mybatis/test/MyBatisTest.java
public class MyBatisTest { /* sqlSession默认不自动提交 * */ @Test public void testMyBatisInsert() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //测试功能 int i = mapper.insertUser(); // //提交事务 // sqlSession.commit(); System.out.println("result:"+i); } @Test public void testMyBatisUpdate() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //测试功能 mapper.updateUser(); // //提交事务 // sqlSession.commit(); } @Test public void testMyBatisDelete() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //测试功能 mapper.deleteUser(); // //提交事务 // sqlSession.commit(); } @Test public void testMyBatisSelect() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); //获取SqlSessionFactoryBuilder SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); //获取sqlSession SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取mapper接口对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //测试功能 // User user=mapper.getUserById(); // System.out.println(user); List<User> users = mapper.getAllUser(); users.forEach(user -> System.out.println(user)); // //提交事务 // sqlSession.commit(); } }