Mybatis学习
Mybatis与hibernate区别:
Hibernate是一个ORM框架,不需要写sql语句,仅仅是面向对象操作。而Mybatis只是把jdbc进行了封装,仍然需要自己写sql语句,只不过不在程序中写了,在xml配置文件中写。
在3.0之前,这个框架叫ibatis,3.0之后,叫Mybatis。
My first project
第一步,建立个工程,java project就可以
第二步,引入如上jar包
第三步,写配置文件
第四步,写domain类
第五步,写具体实体类的配置文件
sqlMapConfig.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件的位置 -->
<mapper resource="lirui/mybatis/domain/User.xml" />
</mappers>
</configuration>
User.java
package lirui.mybatis.domain;
public class User {
/*
* 下面的名字最好和数据库中的名字相同,这样可以自动的匹配。
*/
private String id;
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
System.out.println("正在通过getId方法获取id的值:" + id);
return id;
}
public void setId(String id) {
System.out.println("正在通过setId方法注入id的值:" + id);
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("正在通过setAddress方法注入address的值:" + address);
this.address = address;
}
@Override
public String toString() {
return "{id:" + id + ",name:" + name + ",address:" + address + "}";
}
}
User.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="lirui.mybatis.domain.User">
<!-- 根据id查询User -->
<!-- id:当前sql语句的唯一标示 parameterType:参数类型 resultType:结果类型(返回值类型)-->
<select id="selectUserById" parameterType="string" resultType="lirui.mybatis.domain.User">
select * from user where id = #{id}
</select>
<!-- 查询所有User -->
<select id="selectAllUsers" resultType="lirui.mybatis.domain.User">
select * from user
</select>
</mapper>
MybatisTest.java
import java.io.InputStream;
import java.util.List;
import lirui.mybatis.domain.User;
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.Before;
import org.junit.Test;
public class MybatisTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws Exception{
String resource = "sqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
//根据id查询User
@Test
public void test1()throws Exception{
SqlSession session = sqlSessionFactory.openSession();
// 第一个参数是根据配置文件里面的配置
User user = session.selectOne("lirui.mybatis.domain.User.selectUserById", "0001");
System.out.println(user);
}
//查询所有User
@Test
public void test2()throws Exception{
SqlSession session = sqlSessionFactory.openSession();
List<User> users = session.selectList("lirui.mybatis.domain.User.selectAllUsers");
for (User user : users) {
System.out.println(user);
}
}
}