使用maven创建一个简单的MyBatis例子
MyBatis是一个流行的数据库框架之一,另外一个是hibernate,但是MyBatis是一个班自动映射的框架,它可以解决hibernate全映射的不足。所以MyBatis比hibernate更加的灵活,因此掌握MyBatis是必要的。
这个例子的测试环境
IDE:IntelliJ IDEA + maven + mysql 5.7
一,创建一个maven项目
eclipse也有quickstart这个模板的
填好GroupId与ArtifactId项目就创建好了
以下就是项目的结构
二,在pom.xml文件添加jar包依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
依赖图
三,创建User类,以及对应的映射文件
user.java
package per.summer.MyBatis.model;
/**
* Created by summer on 17-4-16.
*/
public class User{
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
user.xml映射文件
resources以及mapper文件夹自己创建
<?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="per.summer.MyBatis.model.UserMapper">
<select id="selectUserByID" parameterType="int" resultType="User">
SELECT * FROM user_inf where id = #{id}
</select>
</mapper>
四,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"></properties>
<typeAliases>
<typeAlias alias="User" type="per.summer.MyBatis.model.User"/>
</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/User.xml"/>
</mappers>
</configuration>
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/user
username=root
password=manager
最后就是测试了
Test.java
package per.summer.MyBatis.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 per.summer.MyBatis.model.User;
import java.io.IOException;
import java.io.Reader;
/**
* Created by summer on 17-4-16.
*/
public class Test {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public static void main(String[] args) {
SqlSession session = sqlSessionFactory.openSession();
try {
User user = (User)session.selectOne("selectUserByID", 2);
session.commit();
System.out.println(user.getUsername());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}