、整合Spring+Mybatis框架并实现增删改查功能 2、在实验一的基础上,整合MyBatis,要求数据从数据库查询 3、导入jdbc.properties属性文件 4、导入log4j.xml属性文件 5、编写application-context.xml核心配置文件 6、创建实体类Student(id, name,studentNo, age) 其他属性可自行补充 7、创建对应的Mapper文件并编写SQL,实现增删改查功能 8、创建单元测试类并实现CRUD各项功能
时间: 2024-02-07 08:02:20 浏览: 73
Spring boot+Mybatis整合实现增删改查(适合初学者入门必备也可以做脚手架开发)
5星 · 资源好评率100%
感谢您提供的问题,下面是针对问题的回答:
1. 整合Spring+Mybatis框架并实现增删改查功能:
(1)在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
```
(2)创建数据源配置文件jdbc.properties:
```
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
```
(3)创建log4j.xml日志配置文件:
```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<logger name="com.test">
<level value="debug" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>
```
(4)创建application-context.xml核心配置文件:
```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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="mapperLocations" value="classpath*:com/test/mapper/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven />
</beans>
```
(5)创建实体类Student:
```
public class Student {
private Long id;
private String name;
private String studentNo;
private Integer age;
// 省略getter和setter方法
}
```
(6)创建对应的Mapper文件StudentMapper.xml并编写SQL,实现增删改查功能:
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.StudentMapper">
<select id="getById" parameterType="java.lang.Long" resultType="com.test.entity.Student">
select id, name, student_no as studentNo, age from student where id=#{id}
</select>
<select id="getByName" parameterType="java.lang.String" resultType="com.test.entity.Student">
select id, name, student_no as studentNo, age from student where name=#{name}
</select>
<select id="getAll" resultType="com.test.entity.Student">
select id, name, student_no as studentNo, age from student
</select>
<insert id="save" parameterType="com.test.entity.Student">
insert into student(name, student_no, age) values(#{name}, #{studentNo}, #{age})
</insert>
<update id="update" parameterType="com.test.entity.Student">
update student set name=#{name}, student_no=#{studentNo}, age=#{age} where id=#{id}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from student where id=#{id}
</delete>
</mapper>
```
(7)创建单元测试类StudentMapperTest并实现CRUD各项功能:
```
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
@Transactional
public class StudentMapperTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void testGetById() {
Student student = studentMapper.getById(1L);
assertNotNull(student);
assertEquals("张三", student.getName());
}
@Test
public void testGetByName() {
Student student = studentMapper.getByName("张三");
assertNotNull(student);
assertEquals("张三", student.getName());
}
@Test
public void testGetAll() {
List<Student> studentList = studentMapper.getAll();
assertNotNull(studentList);
assertEquals(3, studentList.size());
}
@Test
public void testSave() {
Student student = new Student();
student.setName("李四");
student.setStudentNo("1002");
student.setAge(20);
studentMapper.save(student);
assertNotNull(student.getId());
}
@Test
public void testUpdate() {
Student student = studentMapper.getById(1L);
student.setName("王五");
studentMapper.update(student);
Student updatedStudent = studentMapper.getById(1L);
assertEquals("王五", updatedStudent.getName());
}
@Test
public void testDeleteById() {
studentMapper.deleteById(1L);
Student student = studentMapper.getById(1L);
assertNull(student);
}
}
```
2. 在实验一的基础上,整合MyBatis,要求数据从数据库查询:
在实验一的基础上,只需要在Mapper文件中编写相应的SQL语句即可从数据库查询数据。
3. 导入jdbc.properties属性文件:
请参考问题1的第2步。
4. 导入log4j.xml属性文件:
请参考问题1的第3步。
5. 编写application-context.xml核心配置文件:
请参考问题1的第4步。
6. 创建实体类Student(id, name,studentNo, age) 其他属性可自行补充:
请参考问题1的第5步。
7. 创建对应的Mapper文件并编写SQL,实现增删改查功能:
请参考问题1的第6步。
8. 创建单元测试类并实现CRUD各项功能:
请参考问题1的第7步。
阅读全文