一篇搞定 spring 所有固定配置(CV快速构建项目)不断升级中!~

8 篇文章 0 订阅

MyBatis

1、MyBatis数据库配置文件 .properties

在resources 中创建 database.properties 文件(名称可改),然后直接粘贴如下内容

jdbc.driver=com.mysql.jdbc.Driver
# 如果使用mysql8.0,增加一个时区 的配置 以上 serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&userUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=wl86662165
  • 如果使用mysql8.0,增加一个时区 的配置 以上 serverTimezone=Asia/Shanghai

2、MyBatis核心配置文件(spring整合前&后)

spring整合MyBatis 《前》.xml <configuration…>

就是连接数据库配置
在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 核心配置文件-->
<configuration>
    <environments default="development">
        <environment id="development">
            <!-- 事务管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&amp;useSSL=false&amp;userUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="wl86662165"/>
            </dataSource>
        </environment>
    </environments>

</configuration>
spring整合MyBatis 《后》.xml <configuration…>

就是连接数据库配置
在resources 中创建 mybatis-config.xml (名称可改) 文件,然后直接粘贴如下内容

  • typeAliases:为取别名,可以选择性使用
  • mappers:映射dao层的mapper.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 核心配置文件-->
<configuration>

	<!--    配置数据源-给spring去做-->
    <typeAliases>
        <package name="com.ctra.pojo"/>
    </typeAliases>


	<!--    一个dao对应一个mapper-->
    <mappers>
        <mapper class="com.ctra.dao.BookMapper"/>
    </mappers>

</configuration>

mapper.xml
<?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.ctra.dao.UserMapper">

</mapper>

3、MyBatis-Spring 整合

spring-dao.xml (JDBC 版本) .xml <beans …>
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--  DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
            我们这里使用Spring 提供的JDBC:org.springframework.jdbc.datasource-->
    <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?serverTimezone=UTC&amp;useSSL=false&amp;userUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="wl86662165"/>
    </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/ctra/mapper/*.xml"/>
    </bean>

    <!-- sqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>


    <!--    将实现类注入到bean中-->
    <bean id="userMapper" class="com.ctra.mapper.UserMapperImpl">
        <property name="sqlSession"  ref="sqlSession"/>
    </bean>

</beans>

Servlet 4.0 web.xml 配置 .xml <web-app…>

<?xml version="1.0" encoding="UTF-8"?>

<web-app
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0"
    metadata-complete="true">
  
</web-app>

beans

1、基本结构 (官方文档:配置bean).xml <beans …>

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->
</beans>

如下内容非拷贝内容,为原理性解释

  • id : bean 的唯一标识符,相遇对象名
  • class : bean 对象所对应的全限定名: 包名+类型
  • name:也是别名,而且比 name 用法更加灵活(支持多别名)
<bean id="userT" class="com.ctra.pojo.UserT" name="user2 u2,u3;t2">
   <property name="name" value="zhangsan"/>
</bean>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值