MyBatisSpring和MyBatis环境做

   起首做Spring和MyBatis:

 

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

两个重要模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器。

控制反转模式(也称作依赖性介入)的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。

容器 (在 Spring 框架中是 IOC 容器) 负责将这些联系在一起。在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起,决定什么时间调用方法。

     
步骤八:配置Spring的applicationContext.xml

    步骤三:定义映射文件IBookDAO.xml

     步骤七:配置Mybatis-config.xml

    步骤五:定义瑟维斯(Service)(Service)的兑现类似

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>01MyBatis</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SpringSSM</artifactId>
    <packaging>war</packaging>
    <name>SpringSSM Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <!--spring JDBCTemplate-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>

        <!--mybatis jar-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.2</version>
        </dependency>

        <!--spring整合mybatis-->
        <!--Mybatis+Spring整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.0</version>
        </dependency>

    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>
public interface IBookService {
    public int addBook(Book book);
}
jdbc.url=jdbc:mysql:///t14
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=1234

    步骤四:定义Service的接口

   步骤一:定义Bookr实体类

       步骤九:测试类

pom.xml的赖包

开赛简介:

 

   MyBatis:

    步骤二:定义接口类IBookDAO

<?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>
    <!--别名的设置-->
    <typeAliases>
        <package name="cn.happy.bean"></package>
    </typeAliases>
    <!--管理小配置-->
    <mappers>
        <package name="cn.happy.dao"></package>
    </mappers>
</configuration>

 

<?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="cn.happy.dao.IBookDAO">
    <insert id="addBook">
        INSERT INTO book(bookname,bookprice) VALUES (#{bookname},#{bookprice})
    </insert>
</mapper>
<?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: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:property-placeholder location="classpath:jdbc.properties"/>
    <!--阿里数据源 不想进入阿里的程序员,不是好厨师-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--1.将MyBatis SqlSessionFactory 配置一下-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!--bookDAO  根据工厂去生成session-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.happy.dao"></property>
    </bean>

    <!--4.service  去绑定内存中看不见的dao对象-->
    <bean id="bookService" class="cn.happy.service.impl.BookServiceImpl">
        <property name="dao" ref="IBookDAO"></property>
    </bean>

    <!--平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入什么东西 事务对象的来源,一定是Connection -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--方式三:AspectJ AOP 方式管理事务-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="addBook" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <!--1.切点:建议配置的越详细越好,如果像是execution(* *..*.*(..))的话会报很长的错-->
        <aop:pointcut id="mypointcut" expression="execution(* *..*..service.*(..))"></aop:pointcut>
        <!--2.顾问-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"></aop:advisor>
        <!--3.切面-->
    </aop:config>
Error creating bean with name 'dataSource': 
Bean with name 'dataSource' has been injected into other beans [transactionManager] in its raw version as part of a circular reference, 
but has eventually been wrapped. This means that said other beans do not use the final version of the bean. 
This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

名为“dataSource”的Bean已经以其原始版本作为循环引用的一部分注入到其他bean [transactionManager]中,但最终已被包装。
即它有可能会寻址到其他的module的Book里的类

</beans>
public interface IBookDAO {
    public int addBook(Book book);
}

  Spring:

MyBatis 1

public class Book {
    private Integer bookid;
    private String bookname;
    private Integer bookprice;

    public Integer getBookid() {
        return bookid;
    }

    public void setBookid(Integer bookid) {
        this.bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public Integer getBookprice() {
        return bookprice;
    }

    public void setBookprice(Integer bookprice) {
        this.bookprice = bookprice;
    }
}
MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及
结果集的检索。
 
MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java
对象)映射成数据库中的记录。

    步骤六:定义jdbc.properties

MyBatis 2

import cn.happy.bean.Book;
import cn.happy.dao.IBookDAO;
import cn.happy.service.IBookService;
public class BookServiceImpl implements IBookService {
    private IBookDAO dao;
    public int addBook(Book book) {
        return dao.addBook(book);
    }
    public IBookDAO getDao() {
        return dao;
    }
    public void setDao(IBookDAO dao) {
        this.dao = dao;
    }
}

    

@Test
    public void Test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        IBookService service = (IBookService) ctx.getBean("bookService");
        Book book = new Book();
        book.setBookname("艾丝凡");
        book.setBookprice(3);
        service.addBook(book);
    }
网站地图xml地图