Mybatis从入门到精通下篇

Mybatis从入门到精通下篇:

输入类型:

输出类型:

ResultMap:

动态sql:

if标签:

where标签:

sql片段:

foreach标签:

关联查询:

以订单作为主体:

一对一查询:

 

新建pojo:

以user为主体:

一对多关联:

Mybatis整合Spring:

 

配置文件:SqlMapConfig.xml:

加载映射文件(其他都不用):

ApplicationContext:(很关键)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:Config/db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 连接池的最大数据库连接数 -->
        <property name="maxActive" value="10" />
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="5" />
    </bean>

    <!-- SqlSessionFactory配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:Config/SqlMapConfig.xml" />
        <!-- 别名包扫描 -->
        <property name="typeAliasesPackage" value="pojo"/>
    </bean>

    <!-- 传统Dao配置,加载实现类 -->
    <bean class="dao.impl.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
  </beans>

 

Dao开发:

推荐下面这种方式:

源码下载:https://github.com/linhj-james/springMybatis

可以的话帮忙star一下,谢谢~

 

猜你喜欢

转载自blog.csdn.net/weixin_37766296/article/details/85293620