1、在IDEA中创建Maven项目
Maven pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaobai</groupId> <artifactId>MyBatis_Maven</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>MyBatis_Maven Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!--MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--MariaDB数据库驱动 --> <dependency> <groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.3.0</version> </dependency> <!-- JUnit单元测试工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> <build> <finalName>MyBatis_Maven</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2、创建数据库
use test; drop table if exists person; create table person ( bId int primary key auto_increment, bName varchar(10) not null )default charset = utf8;
3、添加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="jdbc.properties"/> <!--给类设置别名,简化xml文件--> <!--<typeAliases>--> <!--<typeAlias alias="entity" type="com.xiaobai.entity.CustomerEntity"/>--> <!--<!–扫描该包下的所有类–>--> <!--<package name="com.xiaobai.entity"/>--> <!--</typeAliases>--> <!--类型转换器--> <!--<typeHandlers>--> <!--<typeHandler handler="com.xiaobai.dao.InvoiceDAO"/>--> <!--<package name="com.xiaobai.dao"/>--> <!--</typeHandlers>--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <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="Invoice.xml" /> <mapper resource="PersonMapper.xml" /> </mappers> </configuration>
jdbc.properties 文件内容如下:
driver = org.mariadb.jdbc.Driver url = jdbc:mariadb://localhost:3306/test username = root password = Qi1007..
解释
4、定义操作Person表的映射文件
在resources目录下创建sql映射文件,在目录中创建一个PersonMapper.xml文件,如下图所示:
PersonMapper.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="com.xiaobai.dao.PersonDAO"> <select id="selectOne" resultType="com.xiaobai.entity.Person"> select * from person where bId=#{bId} </select> </mapper>
解释:
5、编写数据访问层
package com.xiaobai.mybatis.dao; import com.xiaobai.mybatis.entiy.Person; public interface PersonDAO{
Person selectOne(int bId);
}
6、编写单元测试类检查代码
package com; import com.xiaobai.dao.InvoiceDAO; import com.xiaobai.dao.PersonDAO; import com.xiaobai.entity.InvoiceEntity; import com.xiaobai.entity.Person; 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 App { public static void main(String[] args) throws IOException { Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sqlSessionFactory.openSession(); PersonDAO personDAO = session.getMapper(PersonDAO.class); System.out.println(personDAO.selectOne(1)); // InvoiceDAO invoiceDAO = session.getMapper(InvoiceDAO.class); // System.out.println(invoiceDAO.selectAll(1)); // InvoiceEntity invoiceEntity = session.selectOne("com.xiaobai.dao.InvoiceDAO.selectOne",1); // System.out.println(invoiceEntity); } }
结果: