Spring注解整合mybatis具体代码
时间: 2023-05-30 19:06:07 浏览: 114
通过Spring Boot整合Mybatis分析自动配置详解
以下是一个简单的Spring注解整合Mybatis的示例代码:
1. 配置文件
在Spring配置文件中需要配置数据源和Mybatis相关的bean:
```xml
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<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>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper" />
</bean>
```
2. 实体类
```java
public class User {
private Integer id;
private String name;
private Integer age;
// getter、setter 略
}
```
3. Mapper接口
```java
public interface UserMapper {
User selectById(Integer id);
List<User> selectAll();
void insert(User user);
void update(User user);
void deleteById(Integer id);
}
```
4. Mapper XML文件
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectById" parameterType="java.lang.Integer" resultType="com.example.entity.User">
select * from user where id = #{id}
</select>
<select id="selectAll" resultType="com.example.entity.User">
select * from user
</select>
<insert id="insert" parameterType="com.example.entity.User">
insert into user(name, age) values(#{name}, #{age})
</insert>
<update id="update" parameterType="com.example.entity.User">
update user set name=#{name}, age=#{age} where id=#{id}
</update>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>
```
5. Service层
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User selectById(Integer id) {
return userMapper.selectById(id);
}
public List<User> selectAll() {
return userMapper.selectAll();
}
public void insert(User user) {
userMapper.insert(user);
}
public void update(User user) {
userMapper.update(user);
}
public void deleteById(Integer id) {
userMapper.deleteById(id);
}
}
```
6. Controller层
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User selectById(@PathVariable Integer id) {
return userService.selectById(id);
}
@GetMapping("/")
public List<User> selectAll() {
return userService.selectAll();
}
@PostMapping("/")
public void insert(@RequestBody User user) {
userService.insert(user);
}
@PutMapping("/")
public void update(@RequestBody User user) {
userService.update(user);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable Integer id) {
userService.deleteById(id);
}
}
```
以上就是一个简单的Spring注解整合Mybatis的示例代码,实现了基本的增删改查功能。
阅读全文