作为常用的ORM框架,MyBatis的重要性毋庸置疑。因此,通过一个实例学习mybatis的使用:
由于只是入门,因此构建java项目或者Java web项目均可:
1、项目框架:
相关依赖
ext{
mybatisVersion = "3.4.1"
log4jVersion = "1.2.17"
junitVersion = "4.11"
}
dependencies{
//spring
compile "org.mybatis:mybatis:$mybatisVersion"
//log
compile "log4j:log4j:$log4jVersion"
//junit
compile "junit:junit:$junitVersion"
//sql server connector
compile 'net.sourceforge.jtds:jtds:1.3.1'
}
2、数据库模型:
3、通过XML配置sqlSessionFactory
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=test_api"/>
<property name="username" value="sa"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/paditang/mapping/userMapper.xml"/>
</mappers>
</configuration>
4、添加对应模型类和mapper类
package com.paditang.domain;
public class User {
private Integer id;
private String name;
private String location;
public User(){}
public User(Integer id, String name, String location) {
super();
this.id = id;
this.name = name;
this.location = location;
}
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 String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", location=" + location
+ "]";
}
}
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,默认为包名+mapper名-->
<mapper namespace="com.paditang.mapping.userMapper">
<!-- id:为调用时的唯一标示,重复时会报错
parameterType:查询时使用的参数类型
resultType:查询返回的结果集类型
-->
<select id="getUser" parameterType="int"
resultType="com.paditang.domain.User">
select * from t_user where id=#{id}
</select>
</mapper>
package com.paditang.MyBatisTest;
import java.io.IOException;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.paditang.domain.User;
public class GetTest {
SqlSessionFactory sessionFactory;
SqlSession session;
@Before
public void prepare(){
String resource = "MyBatis-conf.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
session = sessionFactory.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test(){
User user = (User)session.selectOne("com.paditang.mapping.userMapper.getUser",1);
System.out.println(user);
}
@After
public void exit(){
session.close();
}
}
输出成功。
至此,成功使用mybaits查询数据库内数据。
我的源码:https://github.com/caiwuxin/MyBatisLearning