1. First create a maven project
Import-related dependencies in the pom file:
Maven
resource
resource的目录下
,则需要通过配置告知Maven 让它把指定目录的配置文件也打包
-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
2. Create your own package in java directory for storing create entity classes
public class Person implements Serializable { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; }
3. Create a table in the database
4. Add MyBatis resources in the master configuration file in which:
<?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> <! - set type alias -> <typeAliases>
<! - Custom Alias -> <!-- <typeAlias type="com.offcn.bean.Person" alias="per" --> <!-- Scan the form All bean types java code scan package When using the current type of mapper.xml, we only need to use the current type or class name is the class name first letter lowercase --> <package name="com.offcn.bean"/> </typeAliases>
<-! Mybatis environment -> <environments default="development"> <environment id="development"> <! - automatically turns on the transaction -> <transactionManager type="JDBC" /> <! - Data Source -> <dataSource type="POOLED"> <Property name = "Driver" value = "com.mysql.jdbc.Driver" />
<-! If it is more than sql8.0 version may need to specify the time zone -> <property name="url" value="jdbc:mysql:///mytest"/>
<property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <-! Mapper.xml scan profiles and Interfaces -> <mappers>
<! - custom configuration file ->
<! - <Resource Mapper = "COM / MyBatis / Mapper / UserMapper.xml" /> ->
<! - scanning loading ->
<name = Package " com.mybatis.mapper ">
</mappers> </configuration>
5. Create a package for storing mapper.xml corresponding interface in java directory
public interface PersonMapper { public List<Person> getPersonAll(); }
6. Add mapper.xml file
<?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.offcn.mapper.PersonMapper"> <resultMap id="BaseResultMap" type="com.offcn.bean.Person"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="age" column="age"></result> </ The resultMap>
<-! method name and the interface must be the same id -> <SELECT id = "getPersonAll" The resultMap = "BaseResultMap"> SELECT * from Person </ SELECT> </mapper>
Note that: If you use the scan to scan mapper.xml profile mapper.xml profiles and corresponding interface should be in the same directory, or will be error, suggesting no mapper.xml this file
Two solutions:
(1) because we've added to make maven configuration files in src / main / java participate packaged in pom file (pom file, I play the following notes in red font content) so we can be placed directly mapper.xml java directory under one package and interface.
(2) or a decentralized java directory and establishing packet interface resource at the same directory structure decentralized mapper.xml directory in the same directory.
Such interfaces will be packaged and stored mapper.xml when we can see the target directory is running in the same directory to see if the configuration file and compile the interface file in the same directory
7. Test
public static void main (String [] args) throws IOException { // a path string stored in the configuration file String config = "mybatisconfig.xml" ; // will flow through configuration files read into memory the InputStream resourceAsStream = Resources.getResourceAsStream (config); // build a SqlSessionFactory (sql session factory) via the plant by constructing a SqlSessionFactory SF = new new the SqlSessionFactoryBuilder () build (resourceAsStream);. // conversation via the factory the SqlSession the session = sf.openSession (); // the session Get byte code file interface, construct a proxy object PersonMapper = session.getMapper PersonMapper (PersonMapper. Class ); // execute SQL statements for data operated by the agent calling the object method List <the Person> personAll = personMapper.getPersonAll (); System.out.println(personAll); // submit Session.commit (); // close the session session.close (); }
Results of the: