创建数据库的一张表 表名为User
我们新建一个普通的maven项目
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>org.west</groupId> 8 <artifactId>MybatisLove</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <!--单元测试--> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>4.11</version> 17 </dependency> 18 19 <!--mybatis的包--> 20 <dependency> 21 <groupId>org.mybatis</groupId> 22 <artifactId>mybatis</artifactId> 23 <version>3.5.1</version> 24 </dependency> 25 26 <!--连接数据库的驱动包--> 27 <dependency> 28 <groupId>mysql</groupId> 29 <artifactId>mysql-connector-java</artifactId> 30 <version>5.1.47</version> 31 </dependency> 32 33 <!--LOG4J日志包--> 34 <dependency> 35 <groupId>log4j</groupId> 36 <artifactId>log4j</artifactId> 37 <version>1.2.17</version> 38 </dependency> 39 40 </dependencies> 41 42 <build> 43 <!--希望maven在导出项目的时,能够将我们的配置及资源导出--> 44 <resources> 45 <resource> 46 <directory>src/main/java</directory> 47 <includes> 48 <include>**/*.properties</include> 49 <include>**/*.xml</include> 50 </includes> 51 <filtering>false</filtering> 52 </resource> 53 <resource> 54 <directory>src/main/resources</directory> 55 <includes> 56 <include>**/*.properties</include> 57 <include>**/*.xml</include> 58 </includes> 59 <filtering>false</filtering> 60 </resource> 61 </resources> 62 </build> 63 64 65 </project>
然后在建一个实体类pojo
1 package org.west.pojo; 2 3 public class User { 4 private int id; 5 private String name; 6 private String pwd; 7 8 public User() { 9 } 10 11 public int getId() { 12 return id; 13 } 14 15 public void setId(int id) { 16 this.id = id; 17 } 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 public String getPwd() { 28 return pwd; 29 } 30 31 public void setPwd(String pwd) { 32 this.pwd = pwd; 33 } 34 35 @Override 36 public String toString() { 37 return "User{" + 38 "id=" + id + 39 ", name='" + name + '\'' + 40 ", pwd='" + pwd + '\'' + 41 '}'; 42 } 43 }
编写一个工具类
1 package org.west.utils; 2 3 import org.apache.ibatis.io.Resources; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 8 import java.io.IOException; 9 import java.io.InputStream; 10 11 public class MybatisUtils { 12 public static SqlSessionFactory sqlSessionFactory; 13 14 static { 15 16 try { 17 String resource="mybatis-config.xml"; 18 InputStream inputStream = Resources.getResourceAsStream(resource); 19 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 20 } catch (IOException e) { 21 e.printStackTrace(); 22 } 23 } 24 public static SqlSessionFactory getSqlSessionFactory(){ 25 26 return sqlSessionFactory; 27 } 28 public static SqlSession getSqlSession(){ 29 30 return sqlSessionFactory.openSession(true); 31 32 } 33 34 }
编写核心配置文件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 <!--数据配置文件--> 7 <properties resource="db.properties"/> 8 9 <!--日志--> 10 <settings> 11 <setting name="logImpl" value="LOG4J"/> 12 </settings> 13 14 <!--别名优化--> 15 <typeAliases> 16 <package name="org.westos.pojo"/> 17 </typeAliases> 18 19 20 <environments default="development"> 21 <environment id="development"> 22 <transactionManager type="JDBC"/> 23 <dataSource type="POOLED"> 24 <property name="driver" value="${driver}"/> 25 <property name="url" value="${url}"/> 26 <property name="username" value="${username}"/> 27 <property name="password" value="${password}"/> 28 </dataSource> 29 </environment> 30 </environments> 31 <mappers> 32 <mapper class="org.west.dao.UserDao"/> 33 </mappers> 34 35 </configuration>
编写接口 使用注解
package org.west.dao; import org.apache.ibatis.annotations.Select; import org.west.pojo.User; import java.util.List; public interface UserDao { @Select("select *from user") List<User> getUserList(); }
编写测试类
package org.west; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import org.west.dao.UserDao; import org.west.pojo.User; import org.west.utils.MybatisUtils; import java.util.List; public class TestDao { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDao mapper = sqlSession.getMapper(UserDao.class); List<User> userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } } }
项目整体目录
运行结果: