1、先创建一个项目,在pom.xml下增加如下jar配置
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.29version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.12version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.12version>
dependency>
2、在src/java下新建一个包com.hgc.pojo,然后在该包下创建一个java文件,文件名User,增加代码如下
package com.hgc.pojo;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3、在src/main/resources下创建一个文件夹mapper,在该文件夹下创建一个UserMapper.xml文件,代码如下
<mapper namespace="com.hgc.pojo.User">
<select id="findById" parameterType="int" resultType="com.hgc.pojo.User">
SELECT * FROM User WHERE id=#{id}
select>
mapper>
4、在src/main/resources下创建mysql.properties文件,代码如下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.9.248:3306/mybatis
jdbc.username=root
jdbc.password=Mysql@123456
5、在src/main/resources下创建mybatis-config.xml文件,代码如下
<configuration>
<properties resource="mysql.properties"/>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
settings>
<typeAliases>
<typeAlias type="com.hgc.pojo.User" alias="User"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
mappers>
configuration>
6、创建一个数据库mybatis,建立一个表User,代码如下
CREATE TABLE `User` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `User` VALUES (1, 'test', 18);
INSERT INTO `User` VALUES (2, '张三', 25);
7、在src/main/java下创建一个包com.hgc.test,在该包下创建UserTest.java文件,代码如下
package test;
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 com.hgc.pojoUser;
import java.io.IOException;
import java.io.Reader;
/**
* Created by 47500 on 2017/8/28.
*/
public class UserTest {
public static void main(String[] args) {
String resource ="mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
User user = session.selectOne("findById",2);
session.commit();
System.out.println(user.getName());
}catch (IOException e){
e.printStackTrace();
}
}
}
8、最后在测试是否有数据输出