-
建立mybatis项目步骤
-
添加依赖包 4个 数据库 Junit mybatis 1.8jdk
-
编写pojo对象(domain)
-
编写核心配置文件(config.xml)
-
编写核心配置文件编写映射文件(sqlMapper.xml)
-
测试框架
-
-
具体实现
-
添加依赖包 4个 数据库 Junit mybatis 1.8jdk
-
在maven中创键项目,用maven管理jar包
-
配置maven pom.xml文件,
- pom.xml源码模板
-
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>cn.kgc</groupId> 8 <artifactId>mybatis03</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <maven.coppiler.source>1.8</maven.coppiler.source> 13 <maven.coppiler.target>1.8</maven.coppiler.target> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>mysql</groupId> 19 <artifactId>mysql-connector-java</artifactId> 20 <version>5.1.6</version> 21 </dependency> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>4.12</version> 26 </dependency> 27 <dependency> 28 <groupId>org.mybatis</groupId> 29 <artifactId>mybatis</artifactId> 30 <version>3.4.5</version> 31 </dependency> 32 </dependencies> 33 34 35 </project>
-
-
编写pojo对象
-
编写与数据库表对应的实体类(在idea中可以直接生成)
-
-
-
编写核心配置文件(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="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
-
编写核心配置文件的映射文件(sqlMapper.xml)
-
sqlMapper.xml模板
-
<?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"> <mapper namespace="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper>
-
测试框架
-
测试文件
-
1 package cn.kgc; 2 3 import cn.kgc.pojo.NewsDetail; 4 import org.apache.ibatis.session.SqlSession; 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 import org.junit.Test; 8 9 import java.io.InputStream; 10 import java.util.List; 11 12 /** 13 * @author liyang 14 * @create 2019/6/28 16:18 15 */ 16 public class TestNewDetail { 17 @Test 18 public void test(){ 19 InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml"); 20 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); 21 SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream); 22 SqlSession sqlSession = build.openSession(); 23 List<Object> objects = sqlSession.selectList("cn.kgc.pojo.findAll"); 24 for (Object o : objects) { 25 NewsDetail newsDetail = (NewsDetail)o; 26 System.out.println(newsDetail); 27 } 28 } 29 }
-
-
转载于:https://www.cnblogs.com/MrliBlog/p/11104273.html