tnblog
首页
视频
资源
登录

SSM整合-Spring整合MyBatis 02

3125人阅读 2022/8/14 18:37 总访问:1450691 评论:0 收藏:0 手机
分类: SpringMVC

续写于

SSM整合-SSM整合流程简介&项目结构搭建 01

一、需求目录

二、
1、pom.xml
1.1、先删除某些自动生成的东西,直到变成如下图类似结构

1.2、导入坐标

  <dependencies>
        <!--    servlet规范-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!--        mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--spring整合jdbc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--  spring整合mybatis     -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--druid 连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!--    分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

            <!--      spring mvc-->
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>5.1.9.RELEASE</version>
            </dependency>

        <!--json的3个坐标-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!--    <dependency>-->
        <!--      <groupId>com.fasterxml.jackson.core</groupId>-->
        <!--      <artifactId>jackson-core</artifactId>-->
        <!--      <version>2.9.0</version>-->
        <!--    </dependency>-->
        <!--    <dependency>-->
        <!--      <groupId>com.fasterxml.jackson.core</groupId>-->
        <!--      <artifactId>jackson-annotations</artifactId>-->
        <!--      <version>2.9.0</version>-->
        <!--    </dependency>-->


        <!--    junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--    spring整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

2、userDao.xml映射文件。注意位置要和dao层的UserDao对应
2.1、搞UserDao.xml映射文件的时候它版在包的层级要先如下图这样建出来再拖成com.itheima.dao的形式


2.2、

<?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.itheima.dao.UserDao">
    <!--配置根据id查询-->
    <select id="get" resultType="user" parameterType="int">select * from user where uuid=#{uuid}</select>
    <!--配置查询所有-->
    <select id="getAll" resultType="user">select * from user</select>
    <!--    根据名称和密码查询-->
    <select id="getByUserNameAndPassword" resultType="user">
        select * from user where userName=#{userName} and password=#{password}
        </select>
    <!--配置保存-->
    <insert id="save" parameterType="user">
        insert into user (userName,password,realName,gender,birthday) values (#{userName },#{password},#{realName},#{gender},#{birthday})
        </insert>

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

    <update id="update" parameterType="user">
update user set userName=#{userName},password=#{password},realName=#{realName},gender=#{gender},birthday=#{birthday} where uuid = #{uuid}
            </update>
</mapper>

2.3、给dao层的UserDao添加注解@Param,如下图

3、实现service层的UserServiceImpl类对应功能
3.1、

3.2、给方法给查询方法添上分页功能

4、创建配置 文件
4.1、创建jdbc.properties。自己数据库的信息这个数据库名称其实

4.2、配置文件,下图少了个Mybatis的映射扫描,但是下方代码有

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--  包扫描-->
    <context:component-scan base-package="com.itheima"/>
    <!--    加载properties文件-->
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <!--数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--    将Mybatis集合到spring-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--        实体类所在的包位置-->
        <property name="typeAliasesPackage" value="com.itheima.domian"/>
    </bean>
    
    <!--    映射扫描-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.itheima.dao"/>
        </bean>
</beans>


评价
没有个性,不需要签名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术