发表于: 2016-05-31 18:28:09
0 1872
环境 java8 tomcat8 idea2016 mysql5.5
使用的库和框架 maven spring4 mybatis3 junit4.11
mapper:
<mapper namespace="me.nemo.dao.StudentDao">
<select id="getById" parameterType="long" resultType="me.nemo.models.Student">
SELECT * FROM student WHERE id=#{id}
</select>
<select id="getStudentList" resultMap="selectStudentList">
SELECT * FROM student
</select>
<resultMap id="selectStudentList" type="me.nemo.models.Student">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="phone" property="phone"/>
<result column="lesson" property="lesson"/>
</resultMap>
<insert id="addStudent" parameterType="me.nemo.models.Student" useGeneratedKeys="true" keyProperty="id">
INSERT INTO student(name,phone,lesson,create_at,update_at) VALUES(#{name},#{phone},#{lesson},#{create_at},#{update_at})
</insert>
<update id="updateStudent" parameterType="me.nemo.models.Student">
UPDATE student SET name=#{name},phone=#{phone},lesson=#{lesson} WHERE id=#{id}
</update>
<delete id="deleteStudent" parameterType="long">
DELETE FROM student where id=#{id}
</delete>
</mapper>
public interface StudentDao {
Student getById(long id);
List<Student> getStudentList();
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudent(long id);
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-context.xml"})
public class TaskOne {
@Autowired
private StudentService studentService;
@Test
public void getById() {
Student student = studentService.getById(3);
System.out.print("id=" + student.getId() + " name=" + student.getName() + " lesson=" + student.getLesson() + " phone=" + student.getPhone());
}
@Test
public void getList() {
List<Student> students = studentService.getStudentList();
students.stream().forEach(student -> System.out.println("id=" + student.getId() + " name=" + student.getName() + " lesson=" + student.getLesson() + " phone=" + student.getPhone())
);
}
@Test
public void addStudent() {
Student student = new Student();
student.setPhone("12345678910");
student.setName("罗伯特");
student.setLesson("C++");
student.setCreate_at(System.currentTimeMillis());
student.setUpdate_at(System.currentTimeMillis());
studentService.addStudent(student);
System.out.println(student.getId());
}
@Test
public void updateStudent() {
Student student = studentService.getById(3);
student.setName("理查德");
student.setLesson("mybatis");
student.setPhone("00000000");
System.out.println(studentService.updateStudent(student));
}
@Test
public void deleteStudent(){
System.out.println(studentService.deleteStudent(6));
}}
测试结果
今天完成:mybatis的配置 spring与mybatis整合 使用aop技术控制事务 spring与junit联合测试
明天计划:学习任务二的内容
评论