CREATE DATABASE mybatis; USE mybatis; CREATE TABLE `user`( `id` INT AUTO_INCREMENT NOT NULL, `name` VARCHAR(20), `pwd` VARCHAR(50), PRIMARY KEY(id) )ENGINE=INNODB DEFAULT CHARSET=utf8;
使用idea创建一个普通的maven项目
导入依赖
<dependencies> <!-- sql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- mybatis依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- junit依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.13</version> </dependency> </dependencies>
在resource目录下创建 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/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
普通的maven项目访问不了src目录下的resources文件
需要在pom.xml里加上
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
这里的工具类帮我们连接了数据库,SqlSession对象类似于Connection对象
package com.qin; 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 utils { private static SqlSessionFactory sqlSessionFactory; static{ //获取sqlSessionFactory对象 try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //使用SqlSessionFactory sqlSessionFactory创建sqlSession实例 //SqlSession完全包含了面向数据库执行sql命令所需要的所有方法 public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
编写UserDao
package com.qin.dao; import com.qin.pojo.User; import java.util.List; public interface UserDao { List<User> getUserList(); }
编写Mapper.xml文件–>类似编写UserDaoImpl
<?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"> <!--类似继承UserDao接口--> <mapper namespace="com.qin.dao.UserDao"> <!-- 实现UserDao里面的方法--> <select id="getUserList" resultType="com.qin.pojo.User"> select * from mybatis.user; </select> </mapper>
在mabatis-config.xml中注册 mapper
<?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/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!-- 注册mapper--> <mappers> <mapper resource="com/qin/dao/UserMapper.xml"/> </mappers> </configuration>
出现这个错误既是没有注册mapper
package com.qin.dao; import com.qin.pojo.User; import com.qin.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void test1(){ //使用工具类获得SqlSesison SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao userDao = sqlSession.getMapper(UserDao.class); List<User> userList = userDao.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } }
在 pom 文件中添加 maven-compiler-plugin 插件,并指定 jdk 使用的 jdk 版本即可解决上面问题。maven-compiler-plugin 的配置同时对 Java compiler 和 Language level 同时生效。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>