准备
MyBatis内部可以帮我们生成dao接口的实现类(代理类,dao接口的代理)
使用这种代理机制的前提是,SqlMapper.xml文件中的namespace必须是dao接口的全限定名称,id必须是dao接口的方法名
开始
创建一个SpringBoot项目,这里我们采用SqlSeeion开发
目录结构如下
1. controller/CarController
@RestController
public class CarController {
@PostMapping("/insert")
public String insert(@RequestBody Car car){
SqlSession sqlSession= SqlSessionUtil.createSqlSession();
CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
carMapper.insert(car);
sqlSession.commit();
return "success";
}
@PostMapping("/delete")
public String delete(Long id){
SqlSession sqlSession= SqlSessionUtil.createSqlSession();
CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
carMapper.delete(id);
sqlSession.commit();
return "success";
}
@PostMapping("/update")
public String update(@RequestBody Car car){
SqlSession sqlSession= SqlSessionUtil.createSqlSession();
CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
carMapper.update(car);
sqlSession.commit();
return "success";
}
@PostMapping("/select")
public Car selectById(Long id){
SqlSession sqlSession= SqlSessionUtil.createSqlSession();
CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
return carMapper.selectById(id);
}
@PostMapping("/selectAll")
public List<Car> selectAll(){
SqlSession sqlSession= SqlSessionUtil.createSqlSession();
CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
return carMapper.selectAll();
}
}
2. mapper/CarMapper
public interface CarMapper {
Integer insert(Car car);
Integer update(Car car);
Integer delete(Long id );
Car selectById(Long id);
List<Car> selectAll();
}
3. pojo/Car
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
private Long id;
private String carNum;
private String brand;
private Double guidePrice;
private String produceTime;
private String carType;
}
4. utils/SqlSessionUtil
public class SqlSessionUtil {
private SqlSessionUtil() {}
private static SqlSessionFactory sqlSessionFactory;
static {
try {
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("ApplicationContext.xml"),"development");
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession createSqlSession() {
return sqlSessionFactory.openSession();
}
}
5. CarMapper.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.example.webapplication.Mapper.CarMapper">
<insert id="insert">
insert into car( car_num, brand, guide_price, produce_time, car_type)
values (#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
</insert>
<update id="update">
update car set
car_num=#{carNum},
brand=#{brand},
guide_price=#{guidePrice},
produce_time=#{produceTime},
car_type=#{carType}
where id=#{id}
</update>
<delete id="delete">
delete from car where id=#{id}
</delete>
<select id="selectById" resultType="com.example.webapplication.pojo.Car">
select
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from car where id=#{id}
</select>
<select id="selectAll" resultType="com.example.webapplication.pojo.Car">
select
id,
car_num as carNum,
brand,
guide_price as guidePrice,
produce_time as produceTime,
car_type as carType
from car
</select>
</mapper>
6. ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="asd60787533"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/webapplication/Mapper/CarMapper.xml"/>
</mappers>
</configuration>