编写一个ssm具有增删改查,动态sql的代码
时间: 2024-05-18 21:15:30 浏览: 15
好的,这是一个简单的SSM项目的增删改查代码示例,包括了动态SQL语句的实现。请注意,这只是一个示例,具体实现方式可能因项目需求而异。
1. 创建数据库表
首先,我们需要在数据库中创建一个表,用于存储我们的数据。这里我们创建一个名为"student"的表,包含id、name和age三个字段。
```sql
CREATE TABLE student (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL
);
```
2. 创建Java类
接下来,我们需要创建Java类来对数据库进行操作。这里我们创建一个名为"Student"的实体类,包含id、name和age三个属性,并为其创建一个相应的Mapper接口。
Student.java:
```java
public class Student {
private int id;
private String name;
private int age;
// 以下为getter和setter方法
}
```
StudentMapper.java:
```java
public interface StudentMapper {
// 新增
int insert(Student student);
// 删除
int deleteById(int id);
// 更新
int update(Student student);
// 查询
List<Student> selectAll();
// 动态SQL查询
List<Student> selectByCondition(Map<String, Object> map);
}
```
3. 配置MyBatis
接下来,我们需要配置MyBatis,以便让Java类与数据库进行交互。在这里,我们使用XML文件来进行配置。
mybatis-config.xml:
```xml
<configuration>
<typeAliases>
<typeAlias type="com.example.demo.entity.Student" alias="Student"/>
</typeAliases>
<mappers>
<mapper resource="com/example/demo/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
```
4. 编写Mapper接口实现类
我们还需要编写一个实现StudentMapper接口的类,用于实现具体的数据库操作。
StudentMapper.xml:
```xml
<mapper namespace="com.example.demo.mapper.StudentMapper">
<insert id="insert" parameterType="Student">
INSERT INTO student (name, age)
VALUES (#{name}, #{age})
</insert>
<delete id="deleteById" parameterType="int">
DELETE FROM student WHERE id = #{id}
</delete>
<update id="update" parameterType="Student">
UPDATE student SET name = #{name}, age = #{age}
WHERE id = #{id}
</update>
<select id="selectAll" resultType="Student">
SELECT * FROM student
</select>
<select id="selectByCondition" resultType="Student">
SELECT * FROM student
<where>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
</mapper>
```
StudentMapperImpl.java:
```java
@Repository
public class StudentMapperImpl implements StudentMapper {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public int insert(Student student) {
return sqlSessionTemplate.insert("insert", student);
}
@Override
public int deleteById(int id) {
return sqlSessionTemplate.delete("deleteById", id);
}
@Override
public int update(Student student) {
return sqlSessionTemplate.update("update", student);
}
@Override
public List<Student> selectAll() {
return sqlSessionTemplate.selectList("selectAll");
}
@Override
public List<Student> selectByCondition(Map<String, Object> map) {
return sqlSessionTemplate.selectList("selectByCondition", map);
}
}
```
5. 在Spring中配置Mapper
最后一步,我们需要在Spring中进行配置,将Mapper接口注入到Spring容器中供其他代码使用。
applicationContext.xml:
```xml
<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*:com/example/demo/mapper/*.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<bean id="studentMapper" class="com.example.demo.mapper.StudentMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
```
至此,我们就完成了一个简单的SSM项目的增删改查以及动态SQL语句的实现。
相关推荐
![](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)
![](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)