发表于: 2020-04-24 23:37:43

1 1364


今天完成的事情:

整合 mybatis 与 spring

项目结构:

Disciple.java

package cn.mogeek.domain;

public class Disciple {

/**
    * 姓名 主修方向 毕业院校 师兄 来源 日报 目标
    */
   private String name, object, graduated_school,
brother, comefrom, daily_report, aims;

/**
    * id 为主键
    */
   private int id, qq;

public void setName(String name) {
this.name = name;
}

public void setObject(String object) {
this.object = object;
}

public void setGraduated_school(String graduated_school) {
this.graduated_school = graduated_school;
}

public void setBrother(String brother) {
this.brother = brother;
}

public void setComefrom(String comefrom) {
this.comefrom = comefrom;
}

public void setDaily_report(String daily_report) {
this.daily_report = daily_report;
}

public void setAims(String aims) {
this.aims = aims;
}

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

public void setQq(int qq) {
this.qq = qq;
}

public String getName() {
return name;
}

public String getObject() {
return object;
}

public String getGraduated_school() {
return graduated_school;
}

public String getBrother() {
return brother;
}

public String getComefrom() {
return comefrom;
}

public String getDaily_report() {
return daily_report;
}

public String getAims() {
return aims;
}

public int getId() {
return id;
}

public int getQq() {
return qq;
}

@Override
   public String toString(){
return "id: " + id
               + ", name: " + name
               + ", object: " + object
               + ", aims: " + aims
               + ", qq: " + qq
               + ", brother: " + brother
               + ", from: " + comefrom
               + ", graduated_school: " + graduated_school
               + ", daily_report: " + daily_report;
}
}

DiscipleDao.java

package cn.mogeek.dao;

import cn.mogeek.domain.Disciple;

import java.util.List;

public interface DiscipleDao {
List<Disciple> query(Disciple disciple) throws Exception;
int insert(Disciple disciple) throws Exception;
int update(Disciple disciple) throws Exception;
int delete(int id) throws Exception;
}

DiscipleDaoImpl.java

package cn.mogeek.dao;

import cn.mogeek.domain.Disciple;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import sun.java2d.windows.GDIWindowSurfaceData;

import java.util.List;

public class DiscipleDaoImpl extends SqlSessionDaoSupport implements DiscipleDao {

public List<Disciple> query(Disciple disciple) throws Exception{
SqlSession sqlSession = this.getSqlSession();
return sqlSession.selectList("mapper.DiscipleMapper.query", disciple);
}

public int insert(Disciple disciple) throws Exception{
SqlSession sqlSession = this.getSqlSession();
return sqlSession.insert("mapper.DiscipleMapper.insert", disciple);
}

public int update(Disciple disciple) throws Exception{
SqlSession sqlSession = this.getSqlSession();
return sqlSession.update("mapper.DiscipleMapper.update", disciple);
}

public int delete(int id) throws Exception{
SqlSession sqlSession = this.getSqlSession();
return sqlSession.delete("mapper.DiscipleMapper.delete", id);
}
}


资源配置:

db.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = 我的密码

mybatis-config.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>
<mappers>
<mapper resource="mapper/DiscipleMapper.xml"></mapper>
</mappers>

</configuration>

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

<!-- 配置数据库相关参数properties的属性:${url} -->
   <context:property-placeholder location="classpath:db.properties" />
<!--配置c3p0连接池-->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置sqlSqlSessionFactory-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入mybatis核心配置文件-->
       <property name="configLocation" value="mybatis-config.xml"></property>
<!--注入数据源-->
       <property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置UserImpl,注入sqlSessionFactory-->
   <bean id="discipleDao" class="cn.mogeek.dao.DiscipleDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>


映射文件:

DiscipleMapper.xml

<?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="mapper.DiscipleMapper">

<resultMap id="discipleMap" type="cn.mogeek.domain.Disciple">
<id property="id" column="id" javaType="int"></id>
<result property="qq" column="qq" javaType="int"></result>
<result property="name" column="name" javaType="String"></result>
<result property="aims" column="aims" javaType="String"></result>
<result property="object" column="object" javaType="String"></result>
<result property="brother" column="brother" javaType="String"></result>
<result property="comefrom" column="comefrom" javaType="String"></result>
<result property="graduated_school" column="graduated_school" javaType="String"></result>
<result property="daily_report" column="daily_report" javaType="String"></result>
</resultMap>

<select id="query" parameterType="cn.mogeek.domain.Disciple" resultType="cn.mogeek.domain.Disciple">
SELECT * FROM jnshu_copy1
<where>
<if test="id neq null and id neq ''">id = #{id}</if>
<if test="qq neq null and qq neq ''">and qq = #{qq}</if>
<if test="name neq null and name neq ''">and name = #{name}</if>
</where>
</select>

<insert id="insert" parameterType="cn.mogeek.domain.Disciple"  useGeneratedKeys="true" keyProperty="id">
insert into jnshu_copy1 (qq, name, object, graduated_school, brother, comefrom, daily_report, aims)
values (#{qq},#{name},#{object},#{graduated_school},#{brother},#{comefrom},#{daily_report},#{aims})
</insert>

<update id="update" parameterType="cn.mogeek.domain.Disciple">
update jnshu_copy1
<set>
<if test="qq != null and qq != ''"> qq = #{qq}, </if>
<if test="name != null and name != ''"> name = #{name}, </if>
<if test="object != null and object != ''"> object = #{object}, </if>
<if test="graduated_school != null and graduated_school != ''"> graduated_school = #{graduated_school}, </if>
<if test="brother != null and brother != ''"> brother = #{brother}, </if>
<if test="comefrom != null and comefrom != ''"> comefrom = #{comefrom}, </if>
<if test="daily_report != null and daily_report != ''"> daily_report = #{daily_report}, </if>
<if test="aims != null and aims != ''"> aims = #{aims}</if>
</set>
where id = #{id}
</update>

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

</mapper>



测试类:

testDiscipleDaoImpl.java

package cn.mogeek.dao;

import cn.mogeek.domain.Disciple;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class testDiscipleDaoImpl {
ApplicationContext applicationContext;
DiscipleDao discipleDao;

@Before
   public void init() throws Exception{
applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
discipleDao = (DiscipleDao)applicationContext.getBean("discipleDao");
}

@Test
   public void testQuery() throws Exception{
Disciple disciple = new Disciple();
disciple.setQq(666666);
List<Disciple> discipleList = discipleDao.query(disciple);
for (Disciple singleOne: discipleList){
System.out.println(singleOne);
}
System.out.println("共查询到结果:" + discipleList.size());
}

@Test
   public void testInsert() throws Exception{
Disciple disciple = new Disciple();
disciple.setQq(788857);
disciple.setObject("无限奥义循环");
disciple.setName("奇异博士-test");
disciple.setComefrom("英国");
disciple.setDaily_report("https://baike.baidu.com/item/%E5%A5%87%E5%BC%82%E5%8D%9A%E5%A3%AB/2492942");
disciple.setBrother("古一法师");
disciple.setAims("打败多玛姆");
disciple.setGraduated_school("格林威治村");
for (int i = 10000; i > 0; i --)
discipleDao.insert(disciple);
}

@Test
   public void testUpdate() throws Exception {
Disciple disciple = new Disciple();
disciple.setId(108813);
disciple.setQq(666666);
disciple.setName("暗灭");
System.out.println(discipleDao.update(disciple));
}

@Test
   public void testDelete() throws Exception{
System.out.println(discipleDao.delete(108813));
}
}

测试都是通过的,结果也符合预期,这里就不贴了。


今天再一次回顾了之前做过的东西,真的忘了好多,以后真的要认真做笔记,很多东西一看以为自己知道了,下次做的时候还是两眼一抹黑。

spring 我了解的还是很少,今天做的 mybatis+spring 与前两天做的纯 mybatis 对比一下还是能够明显感觉到 spring 带来的不同的感觉。

spring 接管了数据源、sqlsession。现在我用 spring 还不太熟练,特别是 bean 的配置。

mybatis只负责映射文件,不得不说 mybatis 处理 sql 真的很强大,今天我又看了关于 映射文件的一些例子,并且修改得到了现在的映射文件,通过 xml 写映射文件感觉莫名的舒服。


明天计划的事情:

1.学习连接池,完成步骤【测试一下不关闭连接池的时候,在Main函数里写1000个循环调用会出现什么情况。

2.学习 bean 的配置

3.直接执行Main方法,去在服务器上跑通流程。

4. 【部署自己服务到服务器上,包括Maven,Mysql客户端等。直接用Maven命令跑单元测试。


遇到的问题:

spring 配置的时候遇到了一些问题


收获:

1.mybatis 编写更加灵活的映射文件


其实 spring+mybatis 这个部分我真的搜了很多资料,总感觉无从下手,根本的原因还是没理解 spring 到底是个什么东西,今天也是硬着头皮把这个做下来了,心里成就感又多了一点。

学习新知识的时候第一步总是难的,踏出第一步感觉就会好多了。加油加油加油!!!


PS:我的师兄什么时候回来给我评日报啊··· 感觉太长时间没写日报,师兄放弃我了。


返回列表 返回列表
评论

    分享到