Mybatis_4.接口类和XML同时使用

时间:2022-02-05 13:26:56

1.实体类User.java

    public class User {
private int id;
private String name;
private int age;
//getter、setter...
}

2.接口类UserMapper.java

public interface UserMapper {
//添加
public int addUser(User user) throws Exception; //删除
public void deleteUser(int id) throws Exception; //修改
public void updateUser(User user) throws Exception; //查询
public User queryUser(int id) throws Exception; //查询所有的用户
public List<User> queryAllUser() throws Exception;
}

3.映射文件userMapper.xml

<mapper namespace="com.test.xmlAndAnno.UserMapper">
<!-- 自定义结果集 -->
<resultMap type="com.test.bean.User" id="userMap">
<id property="id" column="id" javaType="java.lang.Integer"/>
<result property="name" column="name" javaType="java.lang.String"/>
<result property="age" column="age" javaType="java.lang.Integer"/>
</resultMap>
<!-- 增删改查标签的id属性必须和接口中的方法名相同,
id属性值必须是唯一的,不能够重复使用,
parameterType属性表示形参参数类型,
resultType属性表示查询时返回的结果集类型
-->
<!-- userGeneratedKeys(仅对insert有用),会告诉Mybatis使用JDBC的getGeneratedKeys方法来取出由MySQL内部生成的主键,默认值false。
keyProperty(仅对insert有用)标记一个属性,mybatis会通过getGeneratedKey或者通过insert语句的selectKey子元素设置值,默认不设置。
-->
<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="com.test.bean.User">
insert into users (name,age) values (#{name},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id = #{id}
</delete>
<update id="updateUser" parameterType="com.test.bean.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="queryUser" parameterType="int" resultType="com.test.bean.User" >
select * from users where id = #{id}
</select>
<select id="queryAllUser" resultMap="userMap">
select * from users
</select>
</mapper>

4.全局配置文件mybatis.cfg.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="mysql.properties"></properties>
<!-- 配置mybatis运行环境,development:开发模式,work:工作模式-->
<environments default="development">
<environment id="development">
<!-- type=“jdbc”表示使用JDBC的提交和回滚来管理事务 -->
<transactionManager type="JDBC" />
<!-- mybatis提供了三种数据源类型,POOLED,UNPOOLED,JNDI -->
<!-- POOLED:表示支持JDBC数据源的连接池 -->
<!-- UNPOOLED:表示不支持JDBC数据源的连接池 -->
<!-- JNDI:表示支持外部数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 基于注解的增删改查,注册接口,注意Mapper标签的属性为class,引入xml文件是resource -->
<mapper class="com.test.withAnno.UserMapper"/>
</mappers>
<!-- 为实体类定义别名,简化sql映射xml文件中的引用 -->
<!-- <typeAliases>
<typeAlias type="com.test.bean.User" alias="_User"/>
</typeAliases> -->
</configuration>

5.数据库配置文件mysql.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis1?useUincode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

6.测试类

/**
* @author:滕鹏飞
* @version: 1.0
* @Created on: 2018-9-2 下午9:20:14
* 类说明
*/
public class Test { public static void main(String[] args) { //addUser();
//deleteUser();
//updateUser();
queryUser();
queryAllUser();
} //添加
public static void addUser() {
SqlSession session = DBUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User u = new User("张三",21);
try {
mapper.addUser(u);
session.commit();
System.out.println(u.toString());
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
} //删除
public static void deleteUser() {
SqlSession session = DBUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
try {
mapper.deleteUser(2);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
} } //修改
public static void updateUser() {
SqlSession session = DBUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
try {
User user = new User(3,"Tom",24);
mapper.updateUser(user);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
} } //查询
public static void queryUser() {
SqlSession session = DBUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
try {
User user = mapper.queryUser(4);
session.commit();
System.out.println(user.toString());
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
} //查询所有
public static void queryAllUser() {
SqlSession session = DBUtil.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
try {
List<User> users = mapper.queryAllUser();
session.commit();
System.out.println(users);
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
} }

注意:

注意:同时使用接口类和XML文件时,在映射文件中使用的namespace属性,
则在配置文件中只需要引入映射文件即可,如果没有使用namespace属性,
则需要同时引入接口类和映射文件