1.mybatis入门helloworld构建
实体类对应着数据库中的tbl_employe表,Employe类。。。。。。。
package com.atguigu.mybatis.hello;
public class Employe {
private Integer id;
private String lastName;
private String gender;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employe [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]";
}
}
EmployeMapper.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="com.atguigu.mybatis.EmployeMapper">
<!--namespace:名称空间
id:唯一标识
resultType:返回值类型
#<id>:传递过来的参数取出id值
-->
<select id="selectEmp"
resultType="com.atguigu.mybatis.hello.Employe"> select id,last_name lastName,gender,email from tbl_employe
where id= #{id} </select>
</mapper>
全局配置文件
<?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:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 将我们写好的的(EmployeMapper.xml)sql映射文件注射到全局配置文件中,及目前的文件-->
<mappers>
<mapper resource="EmployeMapper.xml" />
</mappers>
</configuration>
测试类MybatisTest
package com.atguigu.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import com.atguigu.mybatis.hello.Employe;
public class MybatisTest {
/* 1.根据xml配置文件(全局配置文件)创建一个sqlsessionFactory对象
* 2.sql映射文件:配置了每一个sql,以及sql的封装规则等。
* 3。将sql映射文件注册在配置文件中,
* 3.写代码。*/
@Test
public void test() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//2.获取sqlSession实例,能直接执行已经映射的sql语句
//sql的唯一标识
//执行sql要用的参数
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession opeSession=sqlSessionFactory.openSession();
try {
//这里的com.atguigu.mybatis.EmployeMapper.selectEmp对应了EmployeMapper.xml中的id
Employe employe=opeSession.selectOne("com.atguigu.mybatis.EmployeMapper.selectEmp", 1);
System.out.println(employe);
}finally {
opeSession.close();
}
}
}
2.mybatis面向接口编程构建helloworld
实体类对应着数据库中的tbl_employe表,Employe类。。。。。。。
package com.atguigu.mybatis.dao;
import com.atguigu.mybatis.hello.Employe;
public interface EmployeMapper {
public Employe getEmployebyid(Integer id);
}
package com.atguigu.mybatis.hello;
public class Employe {
private Integer id;
private String lastName;
private String gender;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employe [id=" + id + ", lastName=" + lastName + ", gender=" + gender + ", email=" + email + "]";
}
}
EmployeMapper.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="com.atguigu.mybatis.dao.EmployeMapper">
<!--namespace:接口类所在位置
id:唯一标识
resultType:返回值类型
#<id>:传递过来的参数取出id值
-->
<select id="getEmployebyid"
resultType="com.atguigu.mybatis.hello.Employe"> select id,last_name lastName,gender,email from tbl_employe
where id= #{id} </select>
</mapper>
全局配置文件
<?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:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 将我们写好的的(EmployeMapper.xml)sql映射文件注射到全局配置文件中,及目前的文件-->
<mappers>
<mapper resource="EmployeMapper.xml" />
</mappers>
</configuration>
package com.atguigu.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import com.atguigu.mybatis.dao.EmployeMapper;
import com.atguigu.mybatis.hello.Employe;
public class MybatisTest {
@BeforeAll
public SqlSession SqlSessionFactoryji() throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession();
}
@Test
public void test1() throws Exception {
SqlSession opeSession=SqlSessionFactoryji();
try {
EmployeMapper employeMapper=opeSession.getMapper(EmployeMapper.class);
Employe employe=employeMapper.getEmployebyid(1);
System.out.println(employe);
System.out.println("sssssssssssssssssssssssssss");
}finally {
opeSession.close();
}
}
}