IDEA 建Mybatis项目
1.项目结构
2.依赖包(pom.xml)
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies>
3.配置jdbc参数(db.properties)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root
4.建Bean(Person.java)
package com.xfishs.pojo; import lombok.Data; import lombok.ToString; @Data //生成get set方法 @ToString //生成toString方法 public class Person { private int id; private String name ; private int age ; }
5.配置mybatis的配置文件(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> <!-- 引入数据库连接配置 --> <properties resource="db.properties"/> <!-- 定义别名 --> <typeAliases> <!-- 方式一 、直接定义 --> <typeAlias type="com.xfishs.pojo.Person" alias="Person"/> <!-- 方式二、 定义包名 --> <!--<package name="com.xfishs.pojo" />--> </typeAliases> <!--定义数据源--> <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="mapper/Person.xml"/> </mappers> </configuration>
6.创建mybatis工具类(MyBatisUtil.java)
package com.xfishs.utils; 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.Reader; public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory = null; static { try { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory= new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } //采用单例模式,私有化构造方法 private MybatisUtil(){} public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
7.配置mapper(Person.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"> <!-- namespace 对应接口类 --> <mapper namespace="com.xfishs.dao.PersonMapper"> <!-- statement --> <!--<insert id="insertPerson" parameterType="Person" >--> <!--insert into person(name,age) values (#{name},#{age})--> <!--</insert>--> <!-- id 对应接口的方法名 parameterType 参数类型, resultType 返回值类型--> <select id="selectPerson" parameterType="int" resultType="Person"> SELECT * FROM PERSON WHERE ID=#{id} </select> </mapper>
8.log4j配置文件(log4j.properties)
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG
9.进行单元测试(Test)
package com.xfishs.dao; import com.xfishs.pojo.Person; import com.xfishs.utils.MybatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import static org.junit.Assert.*; public class PersonMapperTest { @Test public void selectPerson() { SqlSession sqlSession = MybatisUtil.getSqlSession(); PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); Person person = personMapper.selectPerson(1); System.out.println(person); sqlSession.close(); } }
10.测试结果