发表于: 2020-11-08 23:42:51
1 1392
一,今天完成的事情:
其实数据库的表名,不习惯用复数。
前一天已经展示了assert的代码。已经做完了任务要求的18和20。从测试用例想接口,再想实现类。
18.学习Junit,并尝试写自己的第一个单元测试,记着要写在自己的src/main/test下。
20.编写单元测试的代码,注意,你也可以尝试一下,先写单元测试的代码,再写接口,再写实现类。
并且之前的报告已经:使用动态 SQL 最常见情景是根据条件包含 where 子句的一部分。
<select id="selectById" flushCache="true" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from students
where id=#{id, jdbcType=BIGINT}
</select>
如果项目真有需求,按照我自己现在的表student,会有以下写法
<select id="findStudentsName" resultType="Student"> SELECT * FROM students WHERE major = ‘CS’ <if test="student_id != null"> AND student_id like #{studentId} </if></select>
如果是注解写法,对于我的表students:
@UpdateProvider
(type=StudentSqlProvider.
class
, method=
"updateByPrimaryKeySelective"
)
int
updateByPrimaryKeySelective(Student student);
public
String updateByPrimaryKeySelective(Student student) {
BEGIN();
UPDATE(
"students"
);
if
(student.getId() !=
null
) {
SET(
"id = #{id,jdbcType=BIGINT}"
);
}
if
(record.getUsername() !=
null
) {
SET(
"create_at = #{createAt,jdbcType=BIGINT}"
);
}
if
(record.getPassword() !=
null
) {
SET(
"update_at = #{updateAt,jdbcType=BIGINT}"
);
}
if
(record.getNickname() !=
null
) {
SET(
"name = #{name,jdbcType=VARCHAR}"
);
}
WHERE(
"id = #{id,jdbcType=VARCHAR}"
);
return
SQL();
}
为了填充不出问题,写了一些文字处理工具。少复制粘贴。比如
做19里的Spring部分。Spring自己的main方法里调用我程序中Mapper的方法了
19.学习Spring,配置Spring和Junit。Spring MVC分层清楚,喜欢做成这样的
可以不用 SqlSessionFactory、 SqlSession 等对象,因为 MyBatis-Spring 封装。
Add Framework Support
加入各种dependencies
<!--c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--DAO-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<!--Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--json-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
service 代码
package com.nicole.mybatis.service;
import com.nicole.mybatis.entity.Student;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface StudentService {
List<Student> selectAll();
//primary key, only 0 or one result
Student selectById(Long id);
// The result could be null or 1 - max
List<Student> selectByName(String name);
int insertStudent(Student student);
int updateStudentById(long id);
int deleteStudent(long id);
}
package com.nicole.mybatis.service;
import com.nicole.mybatis.entity.Student;
import com.nicole.mybatis.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> selectAll() {
return this.studentMapper.selectAll();
}
@Override
public Student selectById(Long id) {
return this.studentMapper.selectById(id);
}
@Override
public List<Student> selectByName(String name) {
return this.studentMapper.selectByName( name );
}
@Override
public int insertStudent(Student student) {
return this.studentMapper.insertStudent( student );
}
@Override
public int updateStudentById(long id) {
return this.studentMapper.updateStudentById( id );
}
@Override
public int deleteStudent(long id) {
return this.studentMapper.deleteStudent( id );
}
}
<?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:cache="http://www.springframework.org/schema/cache"
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/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.nicole.mybatis.service"></context:component-scan>
</beans>
control
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描@controller注解-->
<context:component-scan base-package="com.nicole.mybatis.control"></context:component-scan>
<!--@RequestMapping生效-->
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
package com.nicole.mybatis.control;
import com.nicole.mybatis.entity.Student;
import com.nicole.mybatis.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/showAllStudent")
public String showAllStudent(Model model)
{
List<Student> studentList = studentService.selectAll();
model.addAttribute("list",studentList);
return "allStudent";
}
}
dao
二,今天问题:
项目可以网页展示结果
三,今天的收获:Spring, MVC
配置dao
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
不用new的方式做
四,明天的计划:
从时间上如果算上这篇,完成至少连续7天交日报的要求了
评论