流程: 环境搭建->加载Mybatis的jar包->编写程序->测试
create datebase `mybatis`; use `mybatis`; drop table if exists `user`; create table `user`( `id` int(20) NOT NULL, `name` varchar(30) DEFAULT NULL, `pwd` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`) ); insert into `user`(`id`,`name`,`pwd`) values (1,'aa','123456'),(2,'bb','abcdef'),(3,'cc','987654');
<!--导入Mybatis依赖--> <dependencies> <!--mysql 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.24</version> </dependency> <!--mybatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--junit--> <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.1</version> <scope>test</scope> </dependency> </dependencies>
XML 配置文件中包含了对 MyBatis 系统的核心设置,包括获取数据库连接实例的数据源(DataSource)以及决定事务作用域和控制方式的事务管理器(TransactionManager)
<?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/mybatis?serverTimezone=UTC&useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 请注意项目里每写一个mapper都需要在这里进行注册!--> <mappers> <mapper resource="com/ctgu/dao/UserMapper.xml"/> </mappers> </configuration>
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得
//构建sqlSessionFactory 生产 sqlSession public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //获取sqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream; inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //SqlSession 提供了在数据库执行 SQL 命令所需的所有方法 //可以通过 SqlSession 实例来直接执行已映射的 SQL 语句 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
public interface UserMapper { List<User> getUserList(); }
<?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/dao interface 中文 --> <mapper namespace="com.ctgu.dao.UserMapper"> <select id="getUserList" resultType="com.ctgu.pojo.User"> select * from user </select> </mapper>
public class UserMapperTest { @Test public void test(){ //第一步获取sqlSession, 使用try-with-resource方法会自动关闭资源 try(SqlSession sqlSession = MybatisUtils.getSqlSession()){ //执行sql UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); for(User user : userList){ System.out.println(user); } } } }
输出结果:
结果根据自己创建的数据库表的内容不同而不同
至此第一个Mybatis程序就完成了
特别感谢遇见狂神说的视频教程
org.apache.ibatis.binding.BindingException: Type interface com.ctgu.dao.UserMapper is not known to the MapperRegistry.
找不到com.ctgu.dao.UserMapper的原因是没有在mybatis-config.xml这个文件中注册mapper
注册即可
在pom.xml文件中加入配置:
<!--在build中配置resources,来防止我们资源导出失败的问题--> <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>
添加完成后 Reload All Maven Projects
重新 run test 即可
Invalid byte 1 of 1-byte UTF-8 sequence
设置idea的默认编码和项目编码:
重启idea即可