一、mybatis的增删改查
1、修改hotelMapper接口
package com.pjf.mybatis.dao; import com.pjf.mybatis.po.Hotel; public interface HotelMapper { public Hotel getHotel(Integer i); public void updateHotel(Hotel hotel); public void insertHotel(Hotel hotel); public void deleteHotel(Integer i); }
2、修改hotelMapper.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.pjf.mybatis.dao.HotelMapper"> <!-- public Hotel getHotel(Integer i); --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select id,hotel_name as hotelName, hotel_address as hotelAddress, price from hotel where id=#{id} </select> <!-- public void updateHotel(Hotel hotel); --> <update id="updateHotel" parameterType="com.pjf.mybatis.po.Hotel"> update hotel set price=#{price} where id =#{id} </update> <!-- public void insertHotel(Hotel hotel); --> <insert id="insertHotel" parameterType="com.pjf.mybatis.po.Hotel"> insert into hotel(id,hotel_name,hotel_address,price) value(#{id},#{hotelName},#{hotelAddress},#{price}) </insert> <!-- public void deleteHotel(Integer i); --> <delete id="deleteHotel"> delete from hotel where id=#{id} </delete> </mapper>
3、测试类
package com.pjf.mybatis; import java.io.IOException; import java.io.InputStream; 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 com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 查 @Test public void getHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); System.out.println(hotelMapper.getClass()); Hotel hotel = hotelMapper.getHotel(1001); System.out.println(hotel); session.close(); } // 改 @Test public void updateHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); //注:这里使用sessionFactory.openSession();需要手动提交sql:session.commit(); //如果这里使用sessionFactory.openSession(true);就可以自动提交 SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); Hotel hotel = new Hotel(1001, "南京玄武饭店", "鼓楼区中央路193号", 997); hotelMapper.updateHotel(hotel); session.commit(); session.close(); } // 增 @Test public void insertHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); Hotel hotel = new Hotel(1005, "南京饭店", "鼓楼区中央路001号", 997); hotelMapper.insertHotel(hotel); session.commit(); session.close(); } // 删 @Test public void deleteHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); hotelMapper.deleteHotel(1005); session.commit(); session.close(); } }
这样就可以实现增删改查了
二、自增主键的获取
修改hotelMapper.xml文件
指定useGeneratedKeys 使用自增主键获得主键值策略,keyProperty 指定对应的主键属性值,也就是mybatis获得主键值后,会传给javaBean中的属性
<?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.pjf.mybatis.dao.HotelMapper"> <!-- public Hotel getHotel(Integer i); --> <select id="getHotel" resultType="com.pjf.mybatis.po.Hotel"> select id,hotel_name as hotelName, hotel_address as hotelAddress, price from hotel where id=#{id} </select> <!-- public void updateHotel(Hotel hotel); --> <update id="updateHotel" parameterType="com.pjf.mybatis.po.Hotel"> update hotel set price=#{price} where id =#{id} </update> <!-- public void insertHotel(Hotel hotel); --> <!-- useGeneratedKeys 使用自增主键获得主键值策略 keyProperty 指定对应的主键属性值,也就是mybatis获得主键值后,会传给javaBean中的属性 --> <insert id="insertHotel" parameterType="com.pjf.mybatis.po.Hotel" useGeneratedKeys="true" keyProperty="id"> insert into hotel(id,hotel_name,hotel_address,price) value(#{id},#{hotelName},#{hotelAddress},#{price}) </insert> <!-- public void deleteHotel(Integer i); --> <delete id="deleteHotel"> delete from hotel where id=#{id} </delete> </mapper>
测试
import java.io.IOException; import java.io.InputStream; 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 com.pjf.mybatis.dao.HotelMapper; import com.pjf.mybatis.po.Hotel; public class TestHotel { public SqlSessionFactory sqlSessionFactory() throws IOException { // mybatis的配置文件 String resource = "mybatis_config.xml"; // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)TestHotel.class.getClassLoader() InputStream is = Resources.getResourceAsStream(resource); // 构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); return sessionFactory; } // 增 @Test public void insertHotel() throws IOException { SqlSessionFactory sessionFactory = sqlSessionFactory(); SqlSession session = sessionFactory.openSession(); HotelMapper hotelMapper = session.getMapper(HotelMapper.class); Hotel hotel = new Hotel(bull, "南京饭店", "鼓楼区中央路001号", 997); hotelMapper.insertHotel(hotel);
//直接打印出自增主键的值 System.out.println(hotel.getId()); session.commit(); session.close(); } }