用ssm写一个简易的增删查改
时间: 2023-10-08 17:11:37 浏览: 93
好的,我可以给你提供一个简单的示例,用SSM框架实现增删查改。
首先,你需要创建一个Maven项目,然后在pom.xml文件中引入SSM框架相关的依赖,例如:
```xml
<!-- Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!-- 数据库连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
```
然后,你需要在Spring配置文件中配置数据源、事务管理器、Mapper扫描器等。例如:
```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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.demo.mapper"/>
</bean>
```
接下来,你需要创建一个实体类,例如:
```java
public class User {
private Integer id;
private String name;
private Integer age;
// getter和setter方法省略
}
```
然后,你需要创建一个Mapper接口和对应的Mapper XML文件,例如:
```java
public interface UserMapper {
User selectById(Integer id);
List<User> selectAll();
void insert(User user);
void update(User user);
void delete(Integer id);
}
```
```xml
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="selectById" parameterType="java.lang.Integer" resultType="com.example.demo.entity.User">
SELECT * FROM user WHERE id=#{id}
</select>
<select id="selectAll" resultType="com.example.demo.entity.User">
SELECT * FROM user
</select>
<insert id="insert" parameterType="com.example.demo.entity.User">
INSERT INTO user(name, age) VALUES(#{name}, #{age})
</insert>
<update id="update" parameterType="com.example.demo.entity.User">
UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
<delete id="delete" parameterType="java.lang.Integer">
DELETE FROM user WHERE id=#{id}
</delete>
</mapper>
```
最后,你需要在Service层中调用Mapper接口中的方法,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUserById(Integer id) {
return userMapper.selectById(id);
}
@Override
public List<User> getAllUsers() {
return userMapper.selectAll();
}
@Override
public void addUser(User user) {
userMapper.insert(user);
}
@Override
public void updateUser(User user) {
userMapper.update(user);
}
@Override
public void deleteUser(Integer id) {
userMapper.delete(id);
}
}
```
这样,你就可以通过Service层来完成对User表的增删查改操作了。同时,你也可以在Controller层中调用Service层中的方法,处理请求和响应。
阅读全文