MyBatis学习笔记(一)创建第一个MyBatis项目
一、新建Maven项目
http://www.mybatis.org/mybatis-3/zh/index.html 该链接为MyBatis官方地址
创建MyBatis项目主要有两种办法,一种是导入jar包,可在github上下载https://github.com/mybatis/mybatis-3/releases。另一种方法就是现在比较常用的,也是官方推荐的maven导入。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
下面我以IDEA来构建一个简单的MyBatis入门案例
无需选择任何模板直接next即可
直接next到finish即可
二、mybatis配置相关xml文件
pom.xml需要加上必须的MyBatis、junit、mysql连接驱动,这些都可以在maven官网上找到https://mvnrepository.com/
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.mybatis.demo</groupId> 8 <artifactId>demo1</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <dependencies> 11 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> 12 <dependency> 13 <groupId>org.mybatis</groupId> 14 <artifactId>mybatis</artifactId> 15 <version>3.5.0</version> 16 </dependency> 17 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 18 <dependency> 19 <groupId>mysql</groupId> 20 <artifactId>mysql-connector-java</artifactId> 21 <version>5.1.47</version> 22 </dependency> 23 <dependency> 24 <groupId>junit</groupId> 25 <artifactId>junit</artifactId> 26 <version>4.12</version> 27 <scope>test</scope> 28 </dependency> 29 </dependencies> 30 31 32 </project>
然后在main目录下的resources文件夹内新建mybatis-config.xml文件用来存放mybatis配置信息,和mapper文件夹用户存放映射文件
下为mybatis-config.xml文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.jdbc.Driver"/> 11 <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/> 12 <property name="username" value="root"/> 13 <property name="password" value="${password}"/> 14 </dataSource> 15 </environment> 16 </environments> 17 <!--映射配置文件路径--> 18 <mappers> 19 <mapper resource="mapper/userMapper.xml"/> 20 </mappers> 21 </configuration>
三、实现mybatis
我这里就使用navicat创建了一个简单的user表
在IDEA中创建一个user实体类,并且生成getter setter方法
1 public class User { 2 private int id; 3 private String username; 4 private String password; 5 6 public int getId() { 7 return id; 8 } 9 10 public void setId(int id) { 11 this.id = id; 12 } 13 14 public String getUsername() { 15 return username; 16 } 17 18 public void setUsername(String username) { 19 this.username = username; 20 } 21 22 public String getPassword() { 23 return password; 24 } 25 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 }
编写userMapper映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <!-- 6 用于定义一个映射文件的根节点 7 namespace属性是用来配置命名空间 8 resultType是sql映射文件中定义返回值类型 9 --> 10 <mapper namespace="com.mybatis.demo.User"> 11 <select id="userList" resultType="com.mybatis.demo.User"> 12 select * from users 13 </select> 14 </mapper>
编写测试类
1 import com.mybatis.demo.User; 2 import org.apache.ibatis.io.Resources; 3 import org.apache.ibatis.session.SqlSession; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 6 import org.junit.Test; 7 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.util.List; 11 12 public class myBatisTestDemo { 13 @Test 14 public void demo1(){ 15 try { 16 //引入配置文件 17 InputStream io=Resources.getResourceAsStream("mybatis-config.xml"); 18 //创建sqlSessionFactory对象 19 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(io); 20 SqlSession sqlSession=sqlSessionFactory.openSession(); 21 List<User> lists=sqlSession.selectList("userList"); 22 System.out.println(lists.size()); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 28 }