spring-boot+mybatis整合简写

it2022-05-05  117

spring与mybatis整合,代码将变得非常优雅!

主要使用的jar包是mybatis-spring-boot-starter.jar,其maven依赖如下,加入到pom.xml文件中

<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>

创建实体类对象:student

创建mapper接口,打上@Mapper注解

@Mapperpublic interface StudentMapper { @Insert("insert into student(s_id,s_name) values(#{id},#{name})") void createStudent(Student student);

@Select("select * from student") @Results({ @Result(property = "id", column = "s_id"), @Result(property = "name", column = "s_name") }) List<Student> getAllStudents();}

使用增删改查注解添加sql语句,映射到抽象方法中,如果方法中使用了list,则使用results和result注解映射list

 

转载于:https://www.cnblogs.com/lenghui/p/7502104.html


最新回复(0)