发表于: 2017-10-19 22:25:38

1 829


今天完成的事

Mybatis+Spring+Mysql+log+juint整合的一个项目(含单元测试,本来是要整理一下写到简书里,但是没编辑完成,和有一些方法没有收集全,先把今天的代码列出来。

整个项目的结构如下。

映射类Student

package com.xiuzhen.domain;

/**
* Created by ${MIND} on 2017/10/18.
*/

public class Student {
private int id;
   private String user_name;
   private int user_qq;
   private String user_school;
   private String will;
   private long create_at;
   private long update_at;

   @Override
   public String toString() {
return "Student{" +
"id=" + id +
", user_name='" + user_name + '\'' +
", user_qq=" + user_qq +
", user_school='" + user_school + '\'' +
", will='" + will + '\'' +
", create_at=" + create_at +
", update_at=" + update_at +
'}';
   }

public int getId() {
return id;
   }

public void setId(int id) {
this.id = id;
   }

public String getUser_name() {
return user_name;
   }

public void setUser_name(String user_name) {
this.user_name = user_name;
   }

public int getUser_qq() {
return user_qq;
   }

public void setUser_qq(int user_qq) {
this.user_qq = user_qq;
   }

public String getUser_school() {
return user_school;
   }

public void setUser_school(String user_school) {
this.user_school = user_school;
   }

public String getWill() {
return will;
   }

public void setWill(String will) {
this.will = will;
   }

public long getCreate_at() {
return create_at;
   }

public void setCreate_at(long create_at) {
this.create_at = create_at;
   }

public long getUpdate_at() {
return update_at;
   }

public void setUpdate_at(long update_at) {
this.update_at = update_at;
   }
}



MyBatis映射文件

<?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.xiuzhen.dao.StudentDao">
   <!--设置domain类和数据库中表的字段一一对应,注意数据库字段和domain类中的字段名称不致,此处一定要!-->
   <resultMap id="BaseResultMap" type="com.xiuzhen.domain.Student">
           <id column="id" property="id" jdbcType="BIGINT" />
           <result column="user_name" property="user_name" jdbcType="VARCHAR" />
           <result column="user_qq" property="user_qq" jdbcType="INTEGER" />
           <result column="user_school" property="user_school" jdbcType="VARCHAR" />
           <result column="will" property="will" jdbcType="VARCHAR" />
           <result column="create_at" property="create_at" jdbcType="BIGINT" />
           <result column="update_at" property="update_at" jdbcType="BIGINT" />

       </resultMap>
       <!-- 查询单条记录 -->
       <select id="selectStudentById" parameterType="int" resultMap="BaseResultMap">
           SELECT * FROM student WHERE id = #{id}
</select>

       <insert id="insertStudent" parameterType="com.xiuzhen.domain.Student">
           <selectKey keyProperty="id" order="AFTER" resultType="int">
               SELECT last_insert_id()
</selectKey>
           insert into student (user_name,user_qq,user_school,will,create_at,update_at) VALUES
(#{user_name},#{user_qq},#{user_school},#{will},#{create_at},#{update_at})
</insert>



   <update id="updateStudent" parameterType="com.xiuzhen.domain.Student">
        update student set user_name=#{user_name}, user_qq=#{user_qq}, user_school=#{user_school}, will=#{will} ,create_at=#{create_at} ,update_at=#{update_at} where id=#{id}
</update>



   <delete id="deleteStudent" parameterType="int">
       delete from student where id =#{id}
</delete>



   </mapper>

StudentDao

package com.xiuzhen.dao;

import com.xiuzhen.domain.Student;
import org.springframework.stereotype.Service;

/**
* Created by ${MIND} on 2017/10/18.
*/
@Service
public interface StudentDao {
public Student selectStudentById(int id);
   public void insertStudent(Student student);
   public void deleteStudent(int id);
   public void updateStudent(Student student);
}

实现层

package com.xiuzhen.service;

import com.xiuzhen.domain.Student;

/**
* Created by ${MIND-ZR} on 2017/10/18.
*/
public interface StudentService {
Student selectStudentById(int id);
   void insertStudent(Student student);
   void deleteStudent(int id);
   void updateStudent(Student student);
}

实现层

package com.xiuzhen.service;

import com.xiuzhen.dao.StudentDao;
import com.xiuzhen.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Created by ${MIND-ZR} on 2017/10/18.
*/
@Service
public class StudentServiceImpl implements StudentService {

@Autowired
       private StudentDao studentDao;
       public Student selectStudentById(int id){
return studentDao.selectStudentById(id);
   }

public void insertStudent(Student student){
studentDao.insertStudent(student);
       }

public void deleteStudent(int id) {
studentDao.deleteStudent(id);
   }

public void updateStudent(Student student) {
studentDao.updateStudent(student);

   }
}

MyBatisConf.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
       <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
               "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

配置Spring的配置文件

<?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:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- 引入jdbc配置文件 -->
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
           <list>
               <value>classpath:jdbc.properties</value>
               <!--要是有多个配置文件,只需在这里继续添加即可 -->
           </list>
       </property>
   </bean>



   <!-- 配置数据源 -->
   <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <!-- 不使用properties来配置 -->
       <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
           <property name="url" value="jdbc:mysql://localhost:3306/learning" />
           <property name="username" value="root" />
           <property name="password" value="christmas258@" /> -->
       <!-- 使用properties来配置 -->
       <property name="driverClassName">
           <value>${jdbc_driverClassName}</value>
       </property>
       <property name="url">
           <value>${jdbc_url}</value>
       </property>
       <property name="username">
           <value>${jdbc_username}</value>
       </property>
       <property name="password">
           <value>${jdbc_password}</value>
       </property>
   </bean>

   <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage"
                 value="com.xiuzhen.dao" />
   </bean>

   <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
   <!--spring的配置文件一定要加classpath-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
       <property name="mapperLocations" value="classpath:com.xiuzhen.dao/StudentMapper.xml"/>
       <property name="configLocation" value="classpath:mybatis-config.xml" />
       <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"
           /> -->
   </bean>

   <!-- 自动扫描注解的bean -->
   <context:component-scan base-package="com.xiuzhen.service" />
     <context:component-scan base-package="com.xiuzhen.dao" />
   <context:component-scan base-package="com.xiuzhen.domain" />
</beans>


测试基类

package com.xiuzhen.baseTest;

import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Created by ${MIND-ZR} on 2017/10/19.
*/
//指定bean注入的配置文件
@ContextConfiguration(locations = {"classpath:application.xml"})
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests {
protected Logger logger = LoggerFactory.getLogger(getClass());
}

测试类

package com.xiuzhen.service;

import com.xiuzhen.baseTest.SpringTestCase;
import com.xiuzhen.domain.Student;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Created by ${MIND-ZR} on 2017/10/19.
*/
public class StudentServiceTest extends SpringTestCase {
@Autowired
   private StudentService studentService;
   Logger logger = Logger.getLogger(StudentServiceTest.class);

   @Test
   public void selectUserByIdTest() {
Student student = studentService.selectStudentById(10);
       System.out.println(student);
       logger.debug("查找结果" + student);

   }

@Test
   public void insertStudentTest() {
Student student = new Student();

       student.setUser_name("李大军");
       student.setUser_qq(101123);
       student.setUser_school("科大");
       student.setWill("ssssss");
       student.setCreate_at(123);
       student.setUpdate_at(123);

       studentService.insertStudent(student);
       System.out.println(student.getId());
   }
@Test
   public void upadateStudentTest() {
Student student = new Student();

       student.setId(559);
       student.setUser_name("王大力");
       student.setUser_qq(101123);
       student.setUser_school("科大");
       student.setWill("ssssss");
       student.setCreate_at(123);
       student.setUpdate_at(123);

       studentService.updateStudent(student);
       System.out.println(student);
   }
@Test
   public void deleteStudentTest(){
Student student=new Student();
       studentService.deleteStudent(560);
      System.out.println("删除"+student.getId()+"的数据");
   }

}

遇到的问题

编程遇到的各种小坑都排掉了。明天我在简书上把我觉得容易遇到的问题列出来。

收获

总算整体的感受了一哈最近学的东西各个零件都干嘛的,虽然还没特别精通,但是有点成就感了~

明天要做的事

把这个单元测试再详细的分析一下,里面好多问题都是为了跑通而修改的,再理解下,就打包上linux

师兄也帮忙看一下上面的单元测试是否满足了任务一的要求,不行的话还需要改哪里。


返回列表 返回列表
评论

    分享到