首先是导入maven依赖然后创建一个mybaties.xml来配置一下,在配置一个可以与数据库经行交互的Utils,再创建几个包,就像我上面那个图一样
1.namespace中要和Dao/Mapper接口对应
<mapper namespace="com.kuang.dao.UserMapper">
这两个小鸟就非常有意思,都是一一对应的
2.
id是接口的方法名称
reslutType:是参数类型
parameterType:是返回值类型
UserMapper
package com.kuang.dao;
import com.kuang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
User Mapper.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.kuang.dao.UserMapper">
<select id="getUserList" resultType="com.kuang.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user(id,name,pwd)value (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="com.kuang.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id} ;
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
然后自己设计测试类测试