Mac下,IntelliJ IDEA+tomcat+maven+spring+mybatis+mysql(mybatis的配置与mysql的连接,web第四课)

继上一篇博客之后,本篇博客在上一篇的基础之上,通过maven配置mybatis,以及与mysql的连接。

  1. 在pom.xml里面加上mybatis的依赖以及mysql连接依赖
	  <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.5</version>
      </dependency>
      <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.3.1</version>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.13</version>
      </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.28</version>
      </dependency>
  1. 在WEB-INF目录下,创建mapping文件夹,用于存放和数据库操作有关的XML文件,这里新建一个SudentMapper.xml,用于简单地读取数据库。
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mvcdemo.dao.StudentMapper">
    <select id="selectByStuId" parameterType="int" resultType="com.mvcdemo.pojo.Student">
        select * from student where stuId = #{stuId}
    </select>
</mapper>
  1. 接下来,在spring文件夹下,新建一个spring-mybatis.xml,用于配置mybatis
<?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:context="http://www.springframework.org/schema/context"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}"/>
        <property name="username" value="${jdbc_username}"/>
        <property name="password" value="${jdbc_password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0"/>
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20"/>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="20"/>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0"/>
        <!-- 获取最大等待时间 -->
        <property name="maxWait" value="60000" />

        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描目录 -->
        <property name="mapperLocations" value="/WEB-INF/mapping/*.xml"/>
    </bean>

    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mvcdemo.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
</beans>
  1. 然后,在web.xml文件里面,将spring-mybatis.xml部署到context里
  <!--部署applicationContext的xml文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/configs/spring/applicationContext.xml,/WEB-INF/configs/spring/spring-mabatis.xml</param-value>
  </context-param>
  1. 在,configs目录下, 新建一个config.properties文件,用于存储与数据库连接有关的用户名和密码以及驱动(这里记得改成自己mysql数据库的用户名及密码,以及数据库的名字,也就是localhost/后面的student,就是数据库的名字)
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost/student?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc_username=root
jdbc_password=admin123
validationQuery=select 1
  1. 在spring目录下的applicationContext.xml文件,将刚刚的与数据库连接有关的文件加载进去
    <!-- 加载数据库配置文件 -->
    <context:property-placeholder  location="/WEB-INF/configs/config.properties"/>
  1. 到这里,数据库的配置就完全结束了,接下来就是java代码的访问了,首先在com.mvcdemo下新建一个包,取名为dao,并新建一个接口,StudentMapper
package com.mvcdemo.dao;

import com.mvcdemo.pojo.Student;

/**
 * 功能描述:通过ID来查询学生
 *
 * @ClassName: StudentMapper
 * @Author: Chentingxuan
 * @Date: 2019-03-03 20:44
 * @Version: V1.0
 */
public interface StudentMapper {
    Student selectByStuId(Integer stuId);
}

  1. 在com.mvcdemo包下找到名字为StudentServiceImpl的类,并修改原有的方法
 @Autowired
    private StudentMapper studentMapper;

    public Student selectByStuId(Integer stuId) {
        return studentMapper.selectByStuId(stuId);
    }
  1. 运行效果如图
    在这里插入图片描述
    以及数据库中保存的数据
    在这里插入图片描述
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值