使用SMM框架开发企业级应用-----mybatis和spring整合

注解式

  首先导入依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.32</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

entity实体类:

public class IAccount implements Serializable {
    private String accountid;       //卡号
    private String accountname;       //所属人姓名
    private double balance;    //卡内余额

    public String getAccountid() {
        return accountid;
    }

    public void setAccountid(String accountid) {
        this.accountid = accountid;
    }

    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

  Dao接口层

@Repositorypublic interface IAccountDao {
    public List<IAccount> getAllAccount();
}

Dao.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.mybatis_spring.dao.IAccountDao">
    <select id="getAllAccount" resultType="IAccount">
        select * from accounts
    </select>
</mapper>

Service业务接口层:

public interface IAccountService {
    public List<IAccount> getAllAccount();
}

ServiceImpl业务接口实现层:

@Service("iAccountService")
public class IAccountServiceImpl implements IAccountService {
    @Resource
    private IAccountDao iAccountDao;
    @Override
    public List<IAccount> getAllAccount() {
        return iAccountDao.getAllAccount();
    }
}

jdbc.properties文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:/
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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--扫描注解-->
    <context:component-scan base-package="com.mybatis_spring"/>
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--扫描dao层-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mybatis_spring.dao.IAccountDao"/>
    </bean>
  <!--

   0&lt;!&ndash;注入Dao层&ndash;&gt;
    <bean id="iAccountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mybatis_spring.dao.IAccountDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>
    &lt;!&ndash;&lt;!&ndash;Service层&ndash;&gt;&ndash;&gt;
    <bean id="iAccountService" class="com.mybatis_spring.service.Impl.IAccountServiceImpl">
        <property name="iAccountDao" ref="iAccountDao"/>
    </bean>-->

</beans>

测试:

public class IAccountTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService iBankService = (IAccountService)ctx.getBean("iAccountService");
        List<IAccount> iAccounts = iBankService.getAllAccount();
        for(IAccount iAccount:iAccounts){
            System.out.println(iAccount.getAccountname());
        }
    }
}

注解式

entity实体类:

public class IAccount implements Serializable {
    private String accountid;       //卡号
    private String accountname;       //所属人姓名
    private double balance;    //卡内余额

    public String getAccountid() {
        return accountid;
    }

    public void setAccountid(String accountid) {
        this.accountid = accountid;
    }

    public String getAccountname() {
        return accountname;
    }

    public void setAccountname(String accountname) {
        this.accountname = accountname;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

Dao层接口类:

public interface IAccountDao {
    public List<IAccount> getAllAccount();
}

 Dao层小配置文件:

<?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.mybatis_spring.dao.IAccountDao">
    <select id="getAllAccount" resultType="IAccount">
        select * from accounts
    </select>
</mapper>

Service接口类:

public interface IAccountService {
    public List<IAccount> getAllAccount();
}

  Service接口实现类:

public class IAccountServiceImpl implements IAccountService {
    @Resource
    private IAccountDao iAccountDao;
    @Override
    public List<IAccount> getAllAccount() {
        return iAccountDao.getAllAccount();
    }
}

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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--扫描注解-->
    <context:component-scan base-package="com.mybatis_spring"/>
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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>
    <!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--加载Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!--扫描dao层-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mybatis_spring.dao.IAccountDao"/>
    </bean>
  <!--

   0&lt;!&ndash;注入Dao层&ndash;&gt;
    <bean id="iAccountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mybatis_spring.dao.IAccountDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
    </bean>
    &lt;!&ndash;&lt;!&ndash;Service层&ndash;&gt;&ndash;&gt;
    <bean id="iAccountService" class="com.mybatis_spring.service.Impl.IAccountServiceImpl">
        <property name="iAccountDao" ref="iAccountDao"/>
    </bean>-->

</beans>

 测试:

public class IAccountTest {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        IAccountService iBankService = (IAccountService)ctx.getBean("iAccountService");
        List<IAccount> iAccounts = iBankService.getAllAccount();
        for(IAccount iAccount:iAccounts){
            System.out.println(iAccount.getAccountname());
        }
    }
}

原文地址:https://www.cnblogs.com/haohanwuyin/p/11824103.html

时间: 2024-11-06 10:01:46

使用SMM框架开发企业级应用-----mybatis和spring整合的相关文章

使用SMM框架开发企业级应用-----mybatis注解

@select查詢 @insert添加 @delete刪除 @update修改 @Results自关联 @Results映射 @One UserByRole表: RoleByUser表: @Many RoleByUser表: UserByRole表: 原文地址:https://www.cnblogs.com/haohanwuyin/p/11740304.html

使用SMM框架开发企业级应用-----Spring简介即Spring Ioc

Spring框架简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从S

使用SMM框架开发企业级应用-----初始Mybatis的模糊查询以及自动映射

在学习MyBatis过程中想实现模糊查询,下面列举几种方式:1.用${…}代替#{…}    SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');  2.  bind标签  3. CONCAT Mybatis的自动映射 0x00:引子 在 MyBatis 的映射配置文件中,select 标签查询配置结果集时使用过 resultType 属性,当在 resultType 中定义一个 Java 包装类时,

使用SMM框架开发企业级应用-----打印机案例

Ø 要求:可灵活配置使用彩色墨盒或灰色墨盒 Ø 可灵活配置打印页面的大小 实现步骤 打印机功能的实现依赖于墨盒和纸张(对象间的依赖) 定义Ink和Paper接口 使用Ink接口和Paper接口开发打印机程序 开发Ink接口和Paper接口的实现类:ColorInk,GreyInk和TextPaper 组装打印机,运行调试 测试 原文地址:https://www.cnblogs.com/haohanwuyin/p/11740381.html

使用SMM框架开发企业级应用-----面试题

Spring中的bean 组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean. 简单地讲,bean就是由IoC容器初始化.装配及管理的对象 Spring中的bean默认都是单例的(scope="singleton"默认值) 我们可以设置为多例(scope="prototype") bean的作用域 当scope="singleton"时 Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象

使用SMM框架开发企业级应用-----MVC参数传递

处理乱码关于页面传值到后台和后台传值到页面,首先要解决的是中文乱码 post乱码在web.xml中加入过滤器 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <par

使用SMM框架开发企业级应用-----关联查询

关联映射一对多 以国家和省份对应的一对多关系举例. smbms_role数据库: select u.id,u.userName,u.userRole, r.rid,r.roleName from smbms_user as u,smbms_role as r where u.userRole=r.rid and r.rid=3 SmbmsRole实体类:  Dao层接口:  编写小配置xml文件: 测试: 自连接一对多 数据库: 实体类: public class Category {    

使用SMM框架开发企业级应用-----代理(静态,JDK,CGLIB)

静态代理 步骤一:创建抽象主题(接口) 步骤二:真实业务代码 步骤三:测试 JDK代理 步骤一:创建抽象主题(接口) 步骤二:真实业务代码 步骤三:测试 CGLIB代理 步骤一:代码层 步骤二:测试 public class ServiceCglib { public static void main(String[] args) { //步骤一:目标对象 final Cglib cglib=new Cglib(); //步骤二:生成代理 Enhancer enhancer=new Enhanc

使用SMM框架开发企业级应用-----Spring集合注入和域属性自动注入byName和byType

Spring集合的注入 步骤一:导入依赖 步骤二:创建实体类 步骤三:创建大配置文件 步骤四:测试 域属性自动注入 byName与byType 步骤一:创建两个实体类 public class Student { private Integer stuid; private String stuName; private Teacher teacher; public Teacher getTeacher() { return teacher; } public void setTeacher(