ssm框架写一个增删改查
时间: 2023-07-01 22:16:10 浏览: 138
SSM框架写的一个增删改查,适合初学者看看,
好的,我可以为您提供一个简单的SSM框架的增删改查示例代码。假设您要操作的实体类为User,包含id、name和age三个属性:
1. 首先,在MyBatis的mapper文件中定义SQL语句,例如:
```xml
<!-- 根据id查询用户 -->
<select id="getUserById" parameterType="int" resultType="User">
SELECT * FROM user WHERE id = #{id}
</select>
<!-- 查询所有用户 -->
<select id="getAllUsers" resultType="User">
SELECT * FROM user
</select>
<!-- 添加用户 -->
<insert id="addUser" parameterType="User">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<!-- 根据id更新用户 -->
<update id="updateUserById" parameterType="User">
UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}
</update>
<!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
```
2. 在Spring的配置文件中定义MapperScannerConfigurer,自动扫描mapper接口,并注入到Spring容器中:
```xml
<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
```
3. 定义mapper接口,例如:
```java
public interface UserMapper {
User getUserById(int id);
List<User> getAllUsers();
void addUser(User user);
void updateUserById(User user);
void deleteUserById(int id);
}
```
4. 在Spring的配置文件中定义SqlSessionFactoryBean,连接数据库并注入到Spring容器中:
```xml
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
```
5. 在Spring的配置文件中定义DataSource,连接池管理数据库连接:
```xml
<!-- 配置DataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
```
6. 在Spring的配置文件中定义事务管理器TransactionManager:
```xml
<!-- 配置TransactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
```
7. 在Service层中注入Mapper接口,并实现增删改查的逻辑代码,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(int id) {
return userMapper.getUserById(id);
}
@Override
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
@Override
public void addUser(User user) {
userMapper.addUser(user);
}
@Override
public void updateUserById(User user) {
userMapper.updateUserById(user);
}
@Override
public void deleteUserById(int id) {
userMapper.deleteUserById(id);
}
}
```
8. 在Controller层中处理用户请求,例如:
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get/{id}")
@ResponseBody
public User getUserById(@PathVariable("id") int id) {
return userService.getUserById(id);
}
@RequestMapping("/add")
@ResponseBody
public String addUser(@RequestParam("name") String name, @RequestParam("age") int age) {
User user = new User();
user.setName(name);
user.setAge(age);
userService.addUser(user);
return "success";
}
@RequestMapping("/update")
@ResponseBody
public String updateUserById(@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("age") int age) {
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
userService.updateUserById(user);
return "success";
}
@RequestMapping("/delete/{id}")
@ResponseBody
public String deleteUserById(@PathVariable("id") int id) {
userService.deleteUserById(id);
return "success";
}
@RequestMapping("/list")
@ResponseBody
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
```
以上便是一个简单的SSM框架的增删改查示例代码,希望能够对您有所帮助。
阅读全文