首页 > 其他分享 >spring+mybatis

spring+mybatis

时间:2022-11-08 07:33:22浏览次数:54  
标签:mapper spring UserMapper sqlSession mybatis public

1.导入依赖

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>

 

2.编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

<!--配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

    <!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--关联Mybatis-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/kuang/dao/*.xml"/>
</bean>
<!--注册sqlSessionTemplate , 关联sqlSessionFactory-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <!--利用构造器注入-->
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userDao" class="com.kuang.dao.UserDaoImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>

 

3.编写mybatis配置文件

<?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 -->
    <settings>
        <!-- 打开延迟加载的开关 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 将积极加载改为消极加载(即按需加载) -->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!-- 打开全局缓存开关(二级缓存)默认值就是 true -->
        <setting name="cacheEnabled" value="true"/>
      <!-- Java实体类属性:首字母小写的驼峰式命名 -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> <!-- 别名定义 --> <typeAliases> <package name="cn.wmyskxz.pojo"/> </typeAliases> </configuration>

4.编写mapper接口

 

 

6.编写mapper映射文件

 

7.注册bean 添加Dao

public class UserDaoImpl implements UserMapper {
    //sqlSession不用我们自己创建了,Spring来管理
    private SqlSessionTemplate sqlSession;
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

8.测试

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserMapper mapper = (UserMapper) context.getBean("userDao");
        List<User> user = mapper.selectUser();
        System.out.println(user);
    }

 

 

 

 

    <!-- 加载映射文件 -->
    <mappers>
        <!-- 通过 resource 方法一次加载一个映射文件 -->
        <mapper resource="sqlmap/UserMapper.xml"/>
        <!-- 批量加载mapper -->
        <package name="cn.wmyskxz.mapper"/>
    </mappers>

 

标签:mapper,spring,UserMapper,sqlSession,mybatis,public
From: https://www.cnblogs.com/dzs894330350/p/16868076.html

相关文章

  • 第2-1-4章 SpringBoot整合FastDFS文件存储服务
    目录5SpringBoot整合5.1操作步骤5.2项目依赖5.3客户端开发5.3.1FastDFS配置5.3.2FastDFS配置类5.3.3文件工具类5.3.4文件上传配置5.3.5配置Swagger25.3.6API接口......
  • spring+JDBC
    1.导入依赖<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.3.8</version></dependen......
  • Spring Frameword参考手册
    目录OverviewCoreTestingDataAccessWebServletWebReactiveIntegrationLanguagesAppendixWikiOverviewhistory,designphilosophy,feedback,gettingstarted.Core......
  • mybatis数据源单一
    1.打包方式jar包1.导入依赖<dependencies><!--Mybatis核心--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</v......
  • SpringBoot参考手册
    目录LegalGettingHelpDocumentationOverviewGettingStartedUpgradingSpringBootApplicationsUsingSpringBootCoreFeaturesWebDataIOMessagingContainerImagesPro......
  • SpringBoot IO
    目录1、Caching1.1、SupportedCacheProviders2、Hazelcast3、QuartzScheduler4、SendingEmail5、Validation5.1、验证手机号是否有效示例5.1.1、定义验证注解类5.1.2、......
  • Spring的IoC容器之BeanFactory
    SpringIoC容器之BeanFactory首先,Spring提供了两种容器类型:BeanFactory和ApplicationContext。BeanFactory:基础容器类型,提供了完整的IoC服务支持。默认采用延迟......
  • springboot多环境配置
    springboot整合多个环境配置springboot默认读取配置文件名称application,且多个环境配置文件必须按照如下命名Application-dev.yml开发环境application-test.yml......
  • 报错'pom.xml' has syntax errors和Unresolved dependency: 'org.mybatis:mybatis:jar
     错误:'pom.xml'hassyntaxerrors解决方法:构建SpringBoot项目时报错:'pom.xml'hassyntaxerrors大概率是pom.xml中出现了语法错误 <dependency>和</dependency>要......
  • springboot整合nacos config详解
    springboot整合nacos详解@value:读取application文件@ConfigurationProperties:读取指定文件@PropertySource:自定义文件引入@PropertySource+@Value:读取自定义文件@Pro......