MyBatis —— 映射文件
2020-03-12 本文已影响0人
桑鱼nicoo
SELECT
编写 EmployeeMapper.java
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
编写EmployeeMapper.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.sangyu.mapper.EmployeeMapper">
<select id="getEmpById" resultType="com.sangyu.bean.Employee">
select id,last_name,gender,email from tbl_employee where id = #{id}
</select>
</mapper>
INSERT
编写 EmployeeMapper.java
public interface EmployeeMapper {
public void addEmp(Employee employee);
}
编写EmployeeMapper.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.sangyu.mapper.EmployeeMapper">
<!--parameterType,参数类型,可以省略-->
<insert id="addEmp" parameterType="com.sangyu.bean.Employee">
insert into tbl_employee(last_name,email,gender) values(#{lastName},#{email},#{gender});
</insert>
</mapper>
执行测试代码
public class MyBatisTest {
@Test
public void Test02() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession(); // 获取到的SqlSession不会自动提交收据,openSession(true) 自动提交
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = new Employee(null,"GIGI","GIGI@aa.com","0");
mapper.addEmp(employee);
session.commit(); // 手动提交数据
} finally {
session.close();
}
}
}
UPDATE
编写 EmployeeMapper.java
public interface EmployeeMapper {
public void updateEmp(Employee employee);
}
编写EmployeeMapper.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.sangyu.mapper.EmployeeMapper">
<update id="updateEmp">
update tbl_employee set last_name = #{lastName},email=#{email},gender=#{gender}
where id = #{id};
</update>
</mapper>
DELETE
编写 EmployeeMapper.java
public interface EmployeeMapper {
public void deleteEmp(Integer id);
}
编写EmployeeMapper.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.sangyu.mapper.EmployeeMapper">
<delete id="deleteEmp" >
delete from tbl_employee where id = #{id}
</delete>
</mapper>
UPDATE增加返回值
Mybatis 允许增删改直接定义以下类型返回值:Integer、Long、Boolean、void
public interface EmployeeMapper {
public boolean updateEmp(Employee employee);
}
测试
@Test
public void Test05() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = new Employee(13,"AA","AA@aa.com","1");
boolean b = mapper.updateEmp(employee);
System.out.println(b);
} finally {
session.close();
}
}
insert 获得自增主键的值
<?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">
<!--namespace:名称空间,对应mapper接口-->
<mapper namespace="com.sangyu.mapper.EmployeeMapper">
<!--parameterType,参数类型,可以省略
mysql 支持自增主键,自增主键值的后取,mybatis 也是利用statement.getGenreatedKeys();
useGeneratedKeys="true",使用自增主键值策略 keyProperty="id"-->
<insert id="addEmp" parameterType="com.sangyu.bean.Employee" useGeneratedKeys="true">
insert into tbl_employee(last_name,email,gender) values(#{lastName},#{email},#{gender});
</insert>
</mapper>
执行测试代码
@Test
public void Test02() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 根据xml创建sqlSessionFactory
SqlSession session = sqlSessionFactory.openSession(true); // 2. 从 SqlSessionFactory 中获取 SqlSession 的实例,SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句
try {
// 3. 获取接口的实现类对象
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = new Employee(null,"MIMI","MIMI@aa.com","0");
mapper.addEmp(employee);
session.commit(); // 手动提交数据
System.out.println(employee.getId());
} finally {
session.close();
}
}