Mybatis与Spring整合

通过超简单实例了解Mubatis与Spring整合

实例1

采用注解的方式进行查询
创建实体类

public class Person {
        private Integer id;
    private String name;
    private Integer sex;
    private String headUrl;
    private String profession;
    private String introduce;
    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                ", headUrl='" + headUrl + '\'' +
                ", profession='" + profession + '\'' +
                ", introduce='" + introduce + '\'' +
                '}';
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getHeadUrl() {
        return headUrl;
    }

    public void setHeadUrl(String headUrl) {
        this.headUrl = headUrl;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public String getIntroduce() {
        return introduce;
    }

    public void setIntroduce(String introduce) {
        this.introduce = introduce;
    }
}

导入getset方法
创建Dao接口

public interface PersonDao {
    public List<Person> getAll();
}

创建Dao实现类

import java.util.List;
@Repository//用与标注Dao类
public class PersonDaoImpl implements PersonDao {
    @Override
    public List<Person> getAll() {
        return null;
    }
}

创建Service接口

public interface PerviceService {
    public List<Person> getAll();
}

创建Service实现类必须导入set方法

//表示Setvice类
@Service("perviceServiceImpl")
public class PerviceServiceImpl implements PerviceService {
	//默认ByName式,如果name确实默认按照ByType式注入,
    @Resource
    private PersonDao personDao;
    @Override
    public List<Person> getAll() {
        return personDao.getAll();
    }

    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
}

编写database.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///account
jdbc.username=root
jdbc.password=root

编写ApplicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--导入database.properties文件-->
    <context:property-placeholder location="database.properties"/>
    <context:component-scan base-package="com.SpringAndMybatis"/>
    <!--导入数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--配置mybatis核心对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.SpringAndMybatis.entity"></property>
        <property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
    </bean>
    <!--Mybatis的dao接口扫描器(借助dao接口生成动态代理)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指向dao姐接口-->
        <property name="basePackage" value="com.SpringAndMybatis.dao"></property>
    </bean>
</beans>

编写PersonDao.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">
<!--namespace需要指向接口全路径-->
<mapper namespace="com.SpringAndMybatis.dao.PersonDao">
    <select id="getAll" resultType="Person">
        select * from person
    </select>
</mapper>

结果

Person{id=1, name='1', sex=1, headUrl='1', profession='1', introduce='1'}
Person{id=2, name='2', sex=2, headUrl='2', profession='2', introduce='2'}
Person{id=3, name='3', sex=3, headUrl='3', profession='3', introduce='3'}

Process finished with exit code 0

非注解方式

ApplicationContext.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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--导入database.properties文件-->
    <context:property-placeholder location="database.properties"/>
    <context:component-scan base-package="com.SpringAndMybatis"></context:component-scan>
    <!--导入数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--配置mybatis核心对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.SpringAndMybatis.entity"></property>
        <property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
    </bean>
    <!--Mybatis的dao接口扫描器(借助dao接口生成动态代理)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指向dao姐接口-->
        <property name="basePackage" value="com.SpringAndMybatis.dao"></property>
    </bean>
    <!--将service实现类生成到spring容器中-->
    <!--spring隐式注入bytype-->
   <bean id="perviceService" class="com.SpringAndMybatis.Service.impl.PerviceServiceImpl" autowire="byType"/>
    <!--&lt;!&ndash;完成事务配置&ndash;&gt;-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice transaction-manager="transactionManager" id="txAdivce">
        <tx:attributes>
            <tx:method name="getAll" isolation="READ_COMMITTED" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor advice-ref="txAdivce" pointcut="execution(* com.SpringAndMybatis.Service.*.*(..))"/>
    </aop:config>

</beans>

然后吧Service中的@Resource和@Service注释
吧Dao实现类中的@Repository注释就行了
通过注解的方式在Service实现类中不需要set方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值