目录
一、添加用户
(1)持久层接口方法
(2)映射文件
(3)测试方法
二、修改用户
(1)持久层接口方法
(2)映射文件
(3)测试方法
三、删除用户
(1)持久层接口方法
(2)映射文件
(3)测试方法
四、查询用户
(1)持久层接口方法
(2)映射文件
(3)测试方法
五、模糊查询
(1)持久层接口方法
(2)映射文件
(3)测试方法
六、分页查询
(1)持久层接口方法
(2)映射文件
(3)测试方法
七、映射文件注意点
一、添加用户
(1)持久层接口方法
public interface UserMapper { //增加用户 void add(User user); }
(2)映射文件
<?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.gq.mapper.UserMapper"> <!-- 增加用户--> <insert id="add" parameterType="com.gq.pojo.User"> insert into user(username,sex,address) values(#{username},#{sex},#{address}) </insert> </mapper>
(3)测试方法
@Test public void addTest() throws Exception { InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); User user=new User("大明","女","合肥"); userMapper.add(user); session.commit(); session.close(); is.close(); }
二、修改用户
(1)持久层接口方法
public interface UserMapper { //更新用户 void update(User user); }
(2)映射文件
<?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.gq.mapper.UserMapper"> <!-- 更新用户--> <update id="update" parameterType="com.gq.pojo.User"> update user set username=#{username},sex=#{sex},address=#{address} where id=#{id} </update> </mapper>
(3)测试方法
@Test public void updateTest() throws Exception{ InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); User user=new User(7,"唐三","男","北京"); userMapper.update(user); session.commit(); session.close(); is.close(); }
三、删除用户
(1)持久层接口方法
public interface UserMapper { //删除用户 void delete(int userid); }
(2)映射文件
<?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.gq.mapper.UserMapper"> <!--根据id删除用户--> <delete id="delete" parameterType="int"> delete from user where id=#{userid} </delete> </mapper>
(3)测试方法
@Test public void deleteTest() throws Exception{ InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); userMapper.delete(7); session.commit(); session.close(); is.close(); }
四、查询用户
(1)持久层接口方法
public interface UserMapper { //根据id查询用户 User findById(int userid); }
(2)映射文件
<?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.gq.mapper.UserMapper"> <!--根据id查询用户 --> <select id="findById" parameterType="int" resultType="com.gq.pojo.User"> select *from user where id=#{userid} </select> </mapper>
(3)测试方法
@Test public void selectByidTest() throws Exception{ InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); User user=userMapper.findById(3); System.out.println("-------------_______---------------"); System.out.println("username="+user.getUsername()+" address="+user.getAddress()+" sex="+user.getSex()); session.commit(); session.close(); is.close(); }
五、模糊查询
(1)持久层接口方法
List<User> findByNameLike(String username);
(2)映射文件
<select id="findByNameLike" parameterType="string" resultType="com.gq.pojo.User"> select *from user where username like #{username} </select>
(3)测试方法
@Test public void findNameLikeTest() throws Exception{ InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); List<User> userList=userMapper.findByNameLike("%北京%"); for(User user:userList){ System.out.println(user.toString()); } session.commit(); session.close(); is.close(); }
六、分页查询
(1)持久层接口方法
List<User> findPage(@Param("a1") int start, @Param("a2") int size);
对于这里的@Param注解,解释如下:
它可以定义参数名称,让我们在映射文件直接使用名称即可,如果没有他的话,分页查询传递的则为arg0,arg1....或者是param1,param2......并且这两个是死的,不能改
(2)映射文件
<?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.gq.mapper.UserMapper"> <!-- 分页查询--> <select id="findPage" resultType="com.gq.pojo.User"> //因为这里我们前面已经使用@Param注解定义过名字了,不然不可以使用a1,a2 select *from user limit #{a1},#{a2} </select> </mapper>
(3)测试方法
public void findPageTest() throws Exception{ InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(is); SqlSession session=sqlSessionFactory.openSession(); UserMapper userMapper=session.getMapper(UserMapper.class); List<User> userList=userMapper.findPage(0,2); userList.forEach(System.out::println); session.commit(); session.close(); is.close(); }
七、映射文件注意点
对于映射文件中的一些如parameterType等等,具体解释如下。
parameterType:表示参数类型
resultType:表示返回值的类型
对于parameterType,假如是字符串类型,我们将他的值写为string,而不是java.lang.String,为什么呢?
参数/返回值类型为基本数据类型/包装类/String等类型我们可以写全类名也可以写别名文章来源:https://www.toymoban.com/news/detail-707760.html
数据类型 别名 byte _byte long _long short _short int _int int _integer double _double float _float boolean _boolean String string Byte byte Long long Short short Integer int/integer Double double Float float Boolean boolean Date date BigDecimal decimal/bigdecimal Object object Map map HashMap hashmap List list ArrayList arraylist Collection collection Iterator iterator
文章来源地址https://www.toymoban.com/news/detail-707760.html
到了这里,关于Mybatis---增删改查的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!