基本增删改查
增
第一步
在mapper接口中加入方法
package com.mapper;
public interface UserMapper {
//表--实体类 mapper接口--映射文件
int insertUser();//添加数据,返回值为改变的行数
}
第二步
在对应的mapper接口的映射文件中加入相应标签写入sql语句
values()中的参数为添加的表中各个项,这里我的user表格各个项为(userid,username,usertel,userpass,usercard)
<?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接口的路径和名称-->
<mapper namespace="com.mapper.UserMapper">
<!--第二个一致,对应方法名 int insertUser(); -->
<insert id="insertUser">
insert into user values (null,"lurry","2346","1411",26)
</insert>
</mapper>
改
第一步在mapper接口中加入方法
package com.mapper;
public interface UserMapper {
//表--实体类 mapper接口--映射文件
int insertUser();//添加数据
int updateUser();//更改数据
}
第二步,更改数据,在映射文件中加入更新的标签,写sql语句
<?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接口的路径和名称-->
<mapper namespace="com.mapper.UserMapper">
<!--第二个一致,对应方法名 int insertUser(); -->
<insert id="insertUser">
insert into user values (null,"lurry","2346","1411",26)
</insert>
<!--int updateUser();//更改数据-->
<update id="updateUser">
update user set username="giegie" where userid=1
</update>
</mapper>
查
第一步,加入方法,分为查询所有和,按照要求查询两种,注意此时的返回值就是我们查询到的数据
package com.mapper;
import com.pojo.User;
import java.util.List;
public interface UserMapper {
//表--实体类 mapper接口--映射文件
int insertUser();//添加数据
int updateUser();//更改数据
User selectUserById();//查询单个数据
List<User> selectAllUser(); //查询所有
}
第二步,在对应的映射文件中,加入标签写sql语句,该标签中新加入了resultType属性,该属性用于说明查询的数据类型为那种,一般写为实体类的全类名,我这里是user类的全类名,resultMap标签这里没有使用,用于接收查询的不是与实体类对应的数据时,例如仅查询个别列时
<?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接口的路径和名称-->
<mapper namespace="com.mapper.UserMapper">
<!--第二个一致,对应方法名 int insertUser(); -->
<insert id="insertUser">
insert into user values (null,"lurry","2346","1411",26)
</insert>
<!--int updateUser();//更改数据-->
<update id="updateUser">
update user set username="giegie" where userid=1
</update>
<!-- User selectUserById();//查询单个数据 -->
<select id="selectUserById" resultType="com.pojo.User">
select * from user where userid=11
</select>
<!-- List<User> selectAllUser(); 查询所有-->
<select id="selectAllUser" resultType="com.pojo.User">
select * from user
</select>
</mapper>
删
第一步,在mapper接口加入相应的方法
package com.mapper;
import com.pojo.User;
import java.util.List;
public interface UserMapper {
//表--实体类 mapper接口--映射文件
int insertUser();//添加数据
int updateUser();//更改数据
User selectUserById();//查询单个数据
List<User> selectAllUser(); //查询所有
int deleteUserByaId();//删除数据
}
第二步,在映射文件中加入标签
<?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接口-->
<mapper namespace="com.mapper.UserMapper">
<!--第二个一致,对应方法名 int insertUser(); -->
<insert id="insertUser">
insert into user values (null,"lurry","2346","1411",26)
</insert>
<!--int updateUser();//更改数据-->
<update id="updateUser">
update user set username="giegie" where userid=4
</update>
<!-- User selectUserById();//查询单个数据 -->
<select id="selectUserById" resultType="com.pojo.User">
select * from user where userid=11
</select>
<!-- List<User> selectAllUser(); 查询所有-->
<select id="selectAllUser" resultType="com.pojo.User">
select * from user
</select>
<!--int deleteUserByaId();//删除数据 -->
<delete id="deleteUserByaId">
delete from user where userid=5
</delete>
</mapper>
测试类Test,测试上述功能,新建测试类
package com.test;
import com.getsqlsession.M_SelSession;
import com.mapper.UserMapper;
import com.pojo.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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
//加载核心配置文件
InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
//工厂模式,获取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
//获取sqlSessionFactory
SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is);
//通过工厂获取sqlSession
SqlSession sqlSession=sqlSessionFactory.openSession(true);
//通过sqlSession获取接口对象
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
userMapper.insertUser();
userMapper.updateUser();
User user=userMapper.selectUserById(); //接受查询的单个user
System.out.println(user);
List<User> list= userMapper.selectAllUser();//接受查询的所有user
System.out.println("========================================");
for (User u : list)
{
System.out.println(u);
}
userMapper.deleteUserByaId();
}
}
标签:mapper,--,改查,查询,增删,user,Mybatis,import,com
From: https://www.cnblogs.com/cyqf/p/17159097.html