SSM配置MyBatis步骤

1.添加所需maven依赖

maven仓库:https://mvnrepository.com/

<!--Mybatis-->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.9</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.7</version>
</dependency>

<!--数据库驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

2.resources目录新建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>
    <!--日志-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--实体类别名扫描-->
    <typeAliases>
        <package name="cn.kil.pojo"/>
    </typeAliases>
    <!--注册mapper
     这种方式配置的,需要
      1. 所写的xml配置文件和接口在同一包下(在resources下创建和接口相同的路径名也是一样的【注意:创建路径需要用“/”分割,而不是“.”】)
      2. 在xml配置中的namespace要和接口的全类名一样,id和接口中的方法名一样,parameterType 和接口中方法形参一直。
     -->
    <mappers>
        <package name="cn.kil.dao"/>
    </mappers>
</configuration>

2.resources目录新建db.properties文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/kivm?useSSl=true&useUnicode=true&characterEnding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123456

3.配置spring-dao.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">
    <!--导入数据库连接配置文件-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--数据库链接池-->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <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>

    <!--获取SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!--引入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--mapper自动扫描 Mapper静态代理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--引入SqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--扫描的包-->
        <property name="basePackage" value="cn.kil.dao"/>
    </bean>
</beans>

接下来就能愉快的编写mapper和接口了。当然要把mapper文件放在你扫描的包下

项目结构

图片[1]-SSM配置MyBatis步骤-TOY论坛
© 版权声明
THE END
喜欢就支持以下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容