利用spring整合mybatis做student (增删改查)
时间: 2023-08-21 14:02:47 浏览: 100
好的,我可以为您提供一个简单的示例。
1. 首先,您需要在pom.xml文件中添加以下依赖:
```xml
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
```
2. 创建一个Student类,表示学生信息:
```java
public class Student {
private int id;
private String name;
private int age;
// 省略getter和setter方法
}
```
3. 创建一个StudentMapper接口,定义增删改查方法:
```java
public interface StudentMapper {
void insert(Student student);
void deleteById(int id);
void update(Student student);
Student selectById(int id);
List<Student> selectAll();
}
```
4. 创建一个StudentMapper.xml文件,定义SQL语句:
```xml
<mapper namespace="com.example.demo.mapper.StudentMapper">
<insert id="insert">
insert into student(name, age) values(#{name}, #{age})
</insert>
<delete id="deleteById">
delete from student where id=#{id}
</delete>
<update id="update">
update student set name=#{name}, age=#{age} where id=#{id}
</update>
<select id="selectById" resultType="com.example.demo.entity.Student">
select * from student where id=#{id}
</select>
<select id="selectAll" resultType="com.example.demo.entity.Student">
select * from student
</select>
</mapper>
```
5. 在applicationContext.xml中配置数据源和SqlSessionFactory:
```xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
```
6. 创建StudentMapper的实现类,并注入SqlSession:
```java
@Repository
public class StudentMapperImpl implements StudentMapper {
@Autowired
private SqlSession sqlSession;
@Override
public void insert(Student student) {
sqlSession.insert("com.example.demo.mapper.StudentMapper.insert", student);
}
@Override
public void deleteById(int id) {
sqlSession.delete("com.example.demo.mapper.StudentMapper.deleteById", id);
}
@Override
public void update(Student student) {
sqlSession.update("com.example.demo.mapper.StudentMapper.update", student);
}
@Override
public Student selectById(int id) {
return sqlSession.selectOne("com.example.demo.mapper.StudentMapper.selectById", id);
}
@Override
public List<Student> selectAll() {
return sqlSession.selectList("com.example.demo.mapper.StudentMapper.selectAll");
}
}
```
7. 在调用StudentMapper时,使用@Autowired注入即可:
```java
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentMapper studentMapper;
@PostMapping("/add")
public String add(@RequestBody Student student) {
studentMapper.insert(student);
return "success";
}
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable int id) {
studentMapper.deleteById(id);
return "success";
}
@PutMapping("/update")
public String update(@RequestBody Student student) {
studentMapper.update(student);
return "success";
}
@GetMapping("/getById/{id}")
public Student getById(@PathVariable int id) {
return studentMapper.selectById(id);
}
@GetMapping("/getAll")
public List<Student> getAll() {
return studentMapper.selectAll();
}
}
```
以上就是一个简单的利用spring整合mybatis做student(增删改查)的示例,希望对您有所帮助!
阅读全文