发表于: 2017-12-01 22:01:20

1 793


今天完成的事情:

将Mybatis和Spring结合起来使用.

对应学生表的bean类

package com.mybatis_spring.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

/**
* @author Arike
* Create_at 2017/12/1 14:15
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Long id ;
   private Long create_at;
   private Long update_at;
   private String name;
   private String QQ;
   private String major;
   private Long entry_time;
   private String gra_school;
   private String online_id;
   private String daily_link;
   private String desire;
   private String bro;
   private String know_from;
   //提供除了ID以外的构造方法.
   
   public Student(Long create_at, Long update_at, String name, String QQ, String major, Long entry_time, String gra_school, String online_id, String daily_link, String desire, String bro, String know_from) {
this.create_at = create_at;
       this.update_at = update_at;
       this.name = name;
       this.QQ = QQ;
       this.major = major;
       this.entry_time = entry_time;
       this.gra_school = gra_school;
       this.online_id = online_id;
       this.daily_link = daily_link;
       this.desire = desire;
       this.bro = bro;
       this.know_from = know_from;
   }


//提供除了ID和创建时间的构造方法.
   
   
   public Student(Long update_at, String name, String QQ, String major, Long entry_time, String gra_school, String online_id, String daily_link, String desire, String bro, String know_from) {
this.update_at = update_at;
       this.name = name;
       this.QQ = QQ;
       this.major = major;
       this.entry_time = entry_time;
       this.gra_school = gra_school;
       this.online_id = online_id;
       this.daily_link = daily_link;
       this.desire = desire;
       this.bro = bro;
       this.know_from = know_from;
   }
}

mybatis配置

<?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>
   <typeAliases>
       <!-- 为单个字符取别名-->
       <typeAlias alias="Student" type="com.mybatis_spring.bean.Student"/>
       <!--为整个包取别名,com.mybatis.bean去掉,整个包下的类都可以直接使用类名-->
       <!-- <package name="com.myspring.di.context"/>-->
   </typeAliases>
   <mappers>
       <mapper resource="com/mybatis_spring/dao/studentMapper.xml"/>
   </mappers>
</configuration>

mybatis-spring配置


<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"
      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/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd ">

   <context:component-scan base-package="com.mybatis_spring.dao"/>
   <context:property-placeholder location="druid.properties" system-properties-mode="NEVER"/>
   <bean id="druid" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
       <property name="driverClassName" value="${driverClassName}"/>
       <property name="url" value="${url}"/>
       <property name="username" value="${username}"/>
       <property name="password" value="${password}"/>
       <property name="initialSize" value="${initalSize}"/>
   </bean>

   <!--以下是给mybatis配置,数据库对话工厂,mapper映射-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 指定mybatis的全局配置文件的路径 -->
       <property name="configLocation" value="classpath:com/mybatis_spring/dao/mybatis.xml"/>
       <!-- 数据源 -->
       <property name="dataSource" ref="druid"/>
   </bean>
   <!-- 配置StudentDao -->
   <bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
       <!-- 依赖注入SqlSessionFactory -->
       <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
       <property name="mapperInterface" value="com.mybatis_spring.dao.IStudentDao"/>
   </bean>

</beans>

Dao  mapper配置

<?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.mybatis_spring.dao.IStudentDao">
   <!--通过ID查询-->
   <select id="getStudentById" parameterType="long" resultType="Student">
       select * from `student` where id = #{id}
</select>
   <!--通过名字模糊查询-->
   <select id="getStudentByName" parameterType="String" resultType="Student">
       SELECT * FROM `student` WHERE name like concat(concat('%',#{name}),'%')
</select>
   <!--更新数据,update_at是一个关键时间点-->
   <update id="updateStudent" parameterType="Student">
       UPDATE `student`
<set><!--set标签会去除只有最后一个条件的,-->
            <if test="update_at!=null and update_at!=''">
                update_at=#{update_at},
<if test="name!= null and name!=''">name = #{name},</if>
                <if test="QQ!= null and QQ!=''">QQ = #{QQ},</if>
                <if test="major!= null and major!=''">major = #{major},</if>
                <if test="entry_time!= null ">entry_time = #{entry_time},</if>
                <if test="gra_school!= null and gra_school!=''">gra_school = #{gra_school},</if>
                <if test="online_id!= null and online_id!=''">online_id = #{online_id},</if>
                <if test="daily_link!= null and daily_link!=''">daily_link = #{daily_link},</if>
                <if test="desire!= null and desire!=''">desire = #{desire},</if>
                <if test="bro!= null and bro!=''">bro = #{bro},</if>
                <if test="know_from!= null and know_from!=''">know_from = #{know_from},</if>
            </if>

       </set>
       <where><!--where标签会去除第一个条件的andor-->
           <if test="id!=null">AND id=#{id}</if>
       </where>
   </update>

   <!--插入数据-->
   <insert id="insertStudent" parameterType="Student">
       INSERT INTO `student`(create_at, update_at, name,QQ,major,entry_time,gra_school,online_id,daily_link, desire,
bro, know_from)VALUES
(#{create_at},#{update_at},#{name},#{QQ},#{major},#{entry_time},#{gra_school},#{online_id},#{daily_link},#{desire},#{bro},#{know_from})
<!-- 将插入数据的主键返回,返回到user对象中 -->
       <selectKey keyProperty="id" order="AFTER" resultType="long">
           select last_insert_id()
</selectKey>
   </insert>
   <!--foreach模式将一个数组传入-->
   <delete id="deleteStudent" parameterType="long">
       DELETE FROM `student` WHERE id IN
<foreach collection="array" open="(" separator="," close=")" item="arr">
           #{arr}
</foreach>
   </delete>
</mapper>

mapper对应的dao接口

package com.mybatis_spring.dao;

import com.mybatis_spring.bean.Student;

import java.util.List;

/**
* @author Arike
* Create_at 2017/12/1 14:12
*/
public interface IStudentDao {
//通过ID查询
   Student getStudentById(Long id)throws Exception;
   
   //通过name模糊查询
   List<Student> getStudentByName(String name)throws Exception;
   
   //更新
   void updateStudent(Student student)throws Exception;
   
   //插入
   void insertStudent(Student student)throws Exception;
   
   //删除
   void deleteStudent(Long []arr)throws Exception;
   
}


相应的CRUD测试

package com.mybatis_spring.dao;

import com.mybatis_spring.bean.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Calendar;

/**
* @author Arike
* Create_at 2017/12/1 15:58
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/mybatis_spring/dao/mybaits-spring.xml")
public class IStudentDaoTest {
@Autowired
   ApplicationContext ctx;
   @Autowired
   IStudentDao studentDao;
   @Test
   public void getStudentById() throws Exception {
System.out.println(studentDao.getStudentById(11L));
   }

@Test
   public void getStudentByName() throws Exception {
System.out.println(studentDao.getStudentByName(""));
   }

@Test
   public void updateStudent() throws Exception {
Student s = new Student();
       s.setId(10L);
       s.setUpdate_at(System.currentTimeMillis()/1000);
       s.setName("ask觉得撒");
       studentDao.updateStudent(s);
   }

@Test
   public void insertStudent() throws Exception {
Long creat_at = System.currentTimeMillis()/1000;
       Calendar calendar =Calendar.getInstance();
       calendar.set(2017,12,10,10,30,0);
       studentDao.insertStudent(new Student(creat_at,creat_at,"何人","4456","火星",calendar.getTimeInMillis()/1000,"盲人技校","Java001","www.baidu.com","成为BUG书写员","杨以杰","翻墙过来的"));
   }

@Test
   public void deleteStudent() throws Exception {
studentDao.deleteStudent(new Long[]{12L,13L});
   }

}


今天主要就是融合了mybatis和spring,服务器上跑数据这个明天完成,然后提交任务一,详细的任务总结留在明天的日报.


明天计划的事情:

跑通服务器,并尝试往数据库里存放1亿条数据.

遇到的问题:

在本地数据库跑能连接, 将数据库属性改为服务器的属性就连接失败.暂时没找到解决方法.明天再处理.


收获:

学会了如何融合mybatis和spring


禅道:http://task.ptteng.com/zentao/project-burn-414.html  


返回列表 返回列表
评论

    分享到