目录

  • 1 2023广东省精品课程评审申报章节
    • 1.1 申报书
    • 1.2 总结报告
    • 1.3 课程数据信息表
    • 1.4 自评报告
    • 1.5 专家组评语
    • 1.6 相关佐证
  • 2 2020校精品课程评审申报章节
    • 2.1 验收材料总文档
    • 2.2 精品开放课程申报书
    • 2.3 精品开放课程结题验收报告书
    • 2.4 精品开放课程结题验收总结报告
    • 2.5 精品开放课程结题验收项目汇总表
    • 2.6 佐证1.课程基础与建设目标
      • 2.6.1 校本教材
      • 2.6.2 论文
    • 2.7 佐证2.课程设计
      • 2.7.1 课程定位
      • 2.7.2 建设方式
    • 2.8 佐证3.更新与共享
      • 2.8.1 吸引力
      • 2.8.2 内容更新
      • 2.8.3 媒体应用
    • 2.9 佐证4.教学团队
      • 2.9.1 负责人
      • 2.9.2 教学队伍
    • 2.10 佐证5.教学资源
      • 2.10.1 基本资源
        • 2.10.1.1 单元设计
        • 2.10.1.2 课件PPT
        • 2.10.1.3 授课视频
      • 2.10.2 拓展资源
      • 2.10.3 资源创新
    • 2.11 佐证6.课程特色
      • 2.11.1 内容
      • 2.11.2 教学组织
      • 2.11.3 教学模式
    • 2.12 佐证7.目标实现
      • 2.12.1 考评
      • 2.12.2 能学
      • 2.12.3 辅教
  • 3 第一章 Spring 简介
    • 3.1 思政教育
    • 3.2 Sring 概述
    • 3.3 Spring 优点
    • 3.4 Spring 框架结构
    • 3.5 Spring 框架特征
    • 3.6 Spring 入门程序
    • 3.7 Spring 依赖注入
    • 3.8 本章小结
    • 3.9 习题1
  • 4 第二章 Spring中bean的配置和实例化
    • 4.1 思政教育
    • 4.2 Spring中bean的配置
    • 4.3 Bean的实例化
    • 4.4 bean的作用域
    • 4.5 Bean的生命周期
    • 4.6 Bean的装配方式
    • 4.7 应用案例:Annotation注解
    • 4.8 本章小结
    • 4.9 习题2
  • 5 第三章  Spring面向切面编程AOP
    • 5.1 Spring  AOP的基本概念
    • 5.2 通知类型介绍
    • 5.3 JDK动态代理
    • 5.4 Spring AOP
    • 5.5 应用案例:AspectJ开发
    • 5.6 本章小结
    • 5.7 习题3
  • 6 第四章  Mybatis原理
    • 6.1 基本内容
    • 6.2 JDBC的问题
    • 6.3 Hibernate的问题
    • 6.4 MyBatis优势
    • 6.5 MyBatis的工作原理
    • 6.6 MyBatis的数据库准备_MySQL
    • 6.7 应用案例:MyBatis入门程序
    • 6.8 Mybatis数据库基本操作示例
    • 6.9 习题4
  • 7 第五章  Mybatis反向工程
    • 7.1 动态SQL
    • 7.2 应用案例: Mybatis反向(逆向)工程
    • 7.3 Mybatis反向(逆向)工程代码解读
    • 7.4 习题5
  • 8 第六章  Spring、 Mybatis Mapper接口编程
    • 8.1 Mybatis反向工程生成的接口
    • 8.2 应用案例:SpringMVC、 Mybatis  Mapper接口编程
    • 8.3 习题6
  • 9 SpringMVC
    • 9.1 SpringMVC框架
    • 9.2 应用案例:SpringMVC
    • 9.3 应用案例:SpringMVC注解开发
    • 9.4 习题7
  • 10 SpringMVC数据绑定
    • 10.1 SpringMvc的数据绑定流程
    • 10.2 应用案例:SpringMVC参数绑定
    • 10.3 SpringMVC参数绑定数组和集合
    • 10.4 案例SpringMVC参数绑定数组
    • 10.5 习题8
  • 11 第九章、SpringMVC拦截器
    • 11.1 拦截器的实现
    • 11.2 拦截器的配置
    • 11.3 应用案例
    • 11.4 习题9
  • 12 第十章 文件上传
    • 12.1 文件上传原理
    • 12.2 创建工程
    • 12.3 应用案例
    • 12.4 习题10
  • 13 第十一章、课程资源管理系统
    • 13.1 系统需求
    • 13.2 开发环境
    • 13.3 数据库设计
    • 13.4 项目程序设计
  • 14 第十二章 职场沟通技能
    • 14.1 职场沟通原则与理念
  • 15 第十三章 如何与上司、同事沟通
    • 15.1 如何与上司、同事沟通
  • 16 第十四章 软件开发团队建设
    • 16.1 企业级应用开发团队组织技巧培训
应用案例:SpringMVC、 Mybatis  Mapper接口编程

6.2 应用案例:SpringMVCMybatis  Mapper接口编程

我们仍然使用第五章的数据库,并使用第五章Mybits反向工程生成的代码,我们编写程序结构如下图:

在这里,包entity和包mapper中的代码均为Mybits反向工程自动生成的代码。其中包含使用Mybatis框架访问数据库的代码。

我们编写三个配置文件applicationContext.xmldb.propertiesmybatis-config.xml,其中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=123456

jdbc.maxTotal=30

jdbc.maxIdle=10

jdbc.initialSize=5

在这里我们仍然设定数据库名称为school,登录名称为root,密码为123456.

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>

<!-- 别名定义 -->

<typeAliases>

<package name="entity" />

</typeAliases>

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

    xmlns:tx="http://www.springframework.org/schema/tx" 

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

    http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd

    http://www.springframework.org/schema/context

    http://www.springframework.org/schema/context/spring-context-4.3.xsd

    http://www.springframework.org/schema/aop

    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <!-- 读取db.properties -->

    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源 -->

<bean id="dataSource" 

                           class="org.apache.commons.dbcp2.BasicDataSource">

<!--数据库驱动 -->

<property name="driverClassName" value="${jdbc.driver}" />

<!--连接数据库的url -->

<property name="url" value="${jdbc.url}" />

<!--连接数据库的用户名 -->

<property name="username" value="${jdbc.username}" />

<!--连接数据库的密码 -->

<property name="password" value="${jdbc.password}" />

<!--最大连接数 -->

<property name="maxTotal" value="${jdbc.maxTotal}" />

<!--最大空闲连接  -->

<property name="maxIdle" value="${jdbc.maxIdle}" />

<!--初始化连接数  -->

<property name="initialSize" value="${jdbc.initialSize}" />

</bean>

     <!-- 事务管理器,依赖于数据源 -->

<bean id="transactionManager" class=

     "org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>


    <!-- 开启事务注解 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 配置MyBatis工厂SqlSessionFactory -->

    <bean id="sqlSessionFactory" 

                           class="org.mybatis.spring.SqlSessionFactoryBean">

         <!--注入数据源 -->

         <property name="dataSource" ref="dataSource" />

         <!--指定核MyBatis心配置文件位置 -->

  <property name="configLocation" value="classpath:mybatis-config.xml" />

</bean>

 

 

 

    <!-- 配置mapper扫描器 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="mapper"/>

</bean>


<bean id="StudentMapper"  class="org.mybatis.spring.mapper.MapperFactoryBean">

        <!-- 指定要映射的mapper接口的全限定名 -->

        <property name="mapperInterface" value="mapper.StudentMapper"></property>

        <!-- 指定sqlSessionFactory -->

        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

    </bean>

    

    <!-- 扫描Service --> 

    <context:component-scan base-package="entity" />

</beans>

applicationContext.xml配置文件中我们导入了db.propertiesmybatis-config.xml两个文件,并且解析其中内容连接数据库和整合mybatis框架。

applicationContext.xml配置文件中我们还配置了mapper扫描器并设定其扫描位置是mapper包。

我们还配置了一个名为StudentMapperbean,该bean对应的接口我们设定为mapper.StudentMapper

接下来我们编写业务类StudentBiz.java如下:

package biz;

import biz.ApplicationContexter;

import entity.Student;

import entity.StudentExample;

import mapper.StudentMapper;

public class StudentBiz {

public void addStudent(Student student){

StudentMapper ud=(StudentMapper)ApplicationContexter.getApplicationContext().getBean("StudentMapper");

ud.insert(student);

}

public void delStudent(int id){

StudentMapper ud=(StudentMapper)ApplicationContexter.getApplicationContext().getBean("StudentMapper");

ud.deleteByPrimaryKey(id);

}

public void delStudentByName(String name){

StudentMapper ud=(StudentMapper)ApplicationContexter.getApplicationContext().getBean("StudentMapper");

StudentExample se=new StudentExample();

se.createCriteria().andStunameLike(name);

ud.deleteByExample(se);

}

}

在业务类StudentBiz.java中我们设计了三个方法,一个是添加学生,两个是删除学生信息,当然我们也可以设计更多的操作学生信息的方法。

在业务类中我们用到了一个名为ApplicationContexter.java的类,该类有一个静态方法getApplicationContext,用来获取一个单例模式的spring容器,其代码如下:

package biz;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContexter {

static ApplicationContext appc=null;

public static ApplicationContext getApplicationContext(){

if(appc==null)appc=new ClassPathXmlApplicationContext("applicationContext.xml");

return appc;

}

}

我们编写测试类如下:

import biz.StudentBiz;

import entity.Student;

public class Testadd {

public static void main(String[] args) {

//Student s=new Student();

//s.setStuname("bob");

//new StudentBiz().addStudent(s);

new StudentBiz().delStudentByName("%a%");

}

}