mybatis用delete标签写update会删除吗
时间: 2024-06-05 14:07:07 浏览: 7
Mybatis中的delete标签用于执行删除操作,而不是更新操作。如果你使用delete标签来执行更新操作,它将会删除符合条件的行。因此,如果你想执行更新操作,你应该使用update标签而不是delete标签。这是因为,虽然Mybatis中的delete标签可以执行任何SQL语句,但是它的语义是删除操作。
相关问题
用mybatis的注解方式写
好的,这里给您展示一下使用MyBatis注解方式编写DAO和Mapper的示例代码:
**1. DAO层代码:**
```java
public interface UserDao {
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(int id);
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int addUser(User user);
@Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}")
int updateUser(User user);
@Delete("DELETE FROM user WHERE id = #{id}")
int deleteUser(int id);
}
```
**2. Mapper层代码:**
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User getUserById(@Param("id") int id);
@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int addUser(User user);
@Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}")
int updateUser(User user);
@Delete("DELETE FROM user WHERE id = #{id}")
int deleteUser(@Param("id") int id);
}
```
以上代码中,使用了MyBatis注解方式编写了DAO接口和Mapper接口。其中,
- @Select:查询操作;
- @Insert:新增操作;
- @Update:修改操作;
- @Delete:删除操作;
- @Options:设置自动生成主键时的属性。
需要注意的是,使用注解方式时,需要在启动类上添加@MapperScan注解,指定Mapper接口所在的包路径,如下所示:
```java
@SpringBootApplication
@MapperScan("com.example.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
这样就可以使用MyBatis的注解方式编写DAO和Mapper了。
用mybatis plus要怎么写
MyBatis Plus 是 MyBatis 的增强工具库,可以在不写 SQL 语句的情况下操作数据库,提高开发效率。下面是使用 MyBatis Plus 进行数据库操作的基本步骤:
1. 引入 MyBatis Plus 的依赖包。
2. 创建实体类,并使用注解 @TableName 指定数据表名,同时使用注解 @TableId 指定主键字段。
3. 创建 Mapper 接口,该接口需要继承 BaseMapper 接口,并指定泛型为实体类。
4. 在 Spring 配置文件中配置数据源,并将 MyBatis Plus 的插件配置到 SqlSessionFactory 中。
5. 在业务逻辑中,调用 Mapper 接口中的方法进行数据库操作。
下面是一个简单的示例:
1. 引入 MyBatis Plus 的依赖包。
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
```
2. 创建实体类。
```java
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String password;
// 省略 getter/setter 方法
}
```
3. 创建 Mapper 接口。
```java
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在 Spring 配置文件中配置数据源和 MyBatis Plus 的插件。
```xml
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.example.entity" />
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor" />
</array>
</property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper" />
</bean>
```
5. 在业务逻辑中,调用 Mapper 接口中的方法进行数据库操作。
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getUserList() {
return userMapper.selectList(null);
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public int addUser(User user) {
return userMapper.insert(user);
}
@Override
public int updateUser(User user) {
return userMapper.updateById(user);
}
@Override
public int deleteUser(Long id) {
return userMapper.deleteById(id);
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)