1 Hello Mybatis
1.1 搭建环境
搭建数据库
CREATE DATABASE `mybatis`;
USE mybatis;
DROP TABLE `user`;
CREATE TABLE `user` (
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(id, `name`, `pwd`) VALUES
(1, '张三', '123456'),
(2, '李四', '163561'),
(3, '王五', '123698');
新建项目:
2.删除 src 目录,将此项目作为一个父工程
3.导入依赖
<!--导入依赖--> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
<?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核心配置文件--> <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/mybaits?characterEncoding=utf-8&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
//获取SqlSession对象 public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //获取SqlSessionFactory对象 String resource= ""; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
-
-
Dao
接口
public interface UserDao { 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绑定一个对应的Dao接口--> <mapper namespace="com.gazikel.dao.UserDao"> <select id="getUserList" resultType="com.gazikel.pojo.User"> select * from mybatis </select> </mapper>
<mappers> <mapper resource="com/gazikel/dao/UserMapper.xml"/> </mappers>
Maven中由于它的约定大于配置,可能遇到配置文件无法被导出或者生效的问题,解决方案如下:
<!--在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>
但是当我添加了这段话在pom.xml中后程序依然报错,考虑有可能是xml中出现了中文注释,删掉即可解决问题。
可能遇到的问题:
-
配置文件没有注册
-
绑定借口错误
-
方法名错误
-
返回类型错误
-
Maven导出资源问题
遇到其他一些问题不要慌,好好检查一下这些地方有没有错误。