mybatis--CURD操作
mybatis基本的增删查改操作
1.编写session工具类
package com.utils; 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; public class MybatisUtils { private static SqlSession session; private static SqlSessionFactory factory; static { InputStream is= null; try { is = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); factory = builder.build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ session=factory.openSession(); return session; } }
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.dao.UserMapper"> <select id="findAll" resultType="com.bean.User"> select * from test_user; </select> <insert id="save" parameterType="com.bean.User" > INSERT INTO test_user(u_name,u_lovel) VALUES(#{uName},#{uLovel}) </insert> <select id="find" resultType="com.bean.User" parameterType="java.lang.Integer"> select * from test_user where u_id=#{uId}; </select> <update id="updateUser" parameterType="com.bean.User"> update test_user set u_lovel = #{uLovel},u_name=#{uName} where u_id=#{uId}; </update> <select id="findbyName" resultType="com.bean.User" parameterType="java.lang.String"> select * from test_user where u_name like #{uName}; </select> <select id="findbyName2" resultType="com.bean.User" parameterType="java.lang.String"> select * from test_user where u_name like '%'{$uName} '%'; </select> </mapper>
3.dao层接口类
import com.bean.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper { //@Select(" select * from test_user") List<User> findAll(); int save(User user); User find(Integer i); int updateUser(User user); List<User> findbyName(String s); List<User> findbyName2(String s); }
4.测试类
package com; import com.bean.User; import com.dao.UserMapper; import com.utils.MybatisUtils; 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 org.junit.Test; import java.io.InputStream; import java.util.List; public class MybaitsTest01 { @Test public void m1() throws Exception{ //1.读取主配置文件 InputStream is= Resources.getResourceAsStream("mybatis.xml"); //2.创建SessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(is); //3.使用工厂得到SQLSession对象 SqlSession session = factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 UserMapper mapper = session.getMapper(UserMapper.class); //5.使用带库对象执行方法 List<User> all = mapper.findAll(); for (User user : all) { System.out.println(user); } //6.释放资源 session.close(); is.close(); } //编写工具类查询所有 @Test public void m2(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> all = mapper.findAll(); for (User user : all) { System.out.println(user); } session.close(); } //保存操作 @Test public void m3(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user1 = new User(); user1.setuLovel("谦虚"); user1.setuName("周瑜"); int save = mapper.save(user1); if (save>0){ System.out.println("User保存成功"); }else{ System.out.println("User保存失败"); } session.commit(); session.close(); } //查询单个操作 @Test public void m4(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.find(1); System.out.println(user); session.close(); } //更新测试 @Test public void m5(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.find(1); user.setuLovel("111"); int i = mapper.updateUser(user); if (i>0){ System.out.println("更新成功"); }else{ System.out.println("更新失败"); } session.commit(); session.close(); } //模糊查询 @Test public void m6(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); // List<User> users = mapper.findbyName("%角%"); List<User> users = mapper.findbyName("角"); for (User user : users) { System.out.println(user); } session.commit(); session.close(); } }