www.zhblog.net

Spring 与 Mybatis 的初步集成:xml 方式

这篇文章将展示 Spring 与 Mybatis 的集成,以 xml 配置文件的形式实现。

1.准备

Java 1.8

Maven 3


2. pom.xml 

<?xml version="1.0" encoding="UTF-8"?><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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>



    <groupId>com.learn.mybatis</groupId>

    <artifactId>com.learn.mybatis</artifactId>

    <version>1.0-SNAPSHOT</version>



    <properties>

        <java.version>1.8</java.version>

        <springframework.version>4.0.6.RELEASE</springframework.version>

    </properties>



    <dependencies>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>${springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-jdbc</artifactId>

            <version>${springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.4.1</version>

        </dependency>

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis-spring</artifactId>

            <version>2.0.4</version>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.48</version>

        </dependency>

    </dependencies>



    <build>

        <pluginManagement>

            <plugins>

                <plugin>

                    <groupId>org.apache.maven.plugins</groupId>

                    <artifactId>maven-compiler-plugin</artifactId>

                    <version>3.7.0</version>

                    <configuration>

                        <source>1.8</source>

                        <target>1.8</target>

                    </configuration>

                </plugin>

            </plugins>

        </pluginManagement>

        <resources>

            <resource>

                <directory>src/main/java</directory>

                <includes>

                    <include>**/*.xml</include>

                </includes>

            </resource>

        </resources>

    </build>



</project>


3.数据库连接文件 connection.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/blog?useUnicode=true

username=root

password=xxx


4.与数据库表对应的实体类

package com.learn.model;



import java.util.List;



public class Article {

    private Integer articleId;

    private String articlePath;

    private String articleFilename;

    private String articleTitle;



    private Menu menu;



    private List<Comment> comments;



    public Integer getArticleId() {

        return articleId;

    }



    public void setArticleId(Integer articleId) {

        this.articleId = articleId;

    }



    public String getArticlePath() {

        return articlePath;

    }



    public void setArticlePath(String articlePath) {

        this.articlePath = articlePath;

    }



    public String getArticleFilename() {

        return articleFilename;

    }



    public void setArticleFilename(String articleFilename) {

        this.articleFilename = articleFilename;

    }



    public String getArticleTitle() {

        return articleTitle;

    }



    public void setArticleTitle(String articleTitle) {

        this.articleTitle = articleTitle;

    }



    public List<Comment> getComments() {

        return comments;

    }



    public void setComments(List<Comment> comments) {

        this.comments = comments;

    }



    public Menu getMenu() {

        return menu;

    }



    public void setMenu(Menu menu) {

        this.menu = menu;

    }



    @Override

    public String toString() {

        StringBuilder sb = new StringBuilder();

        sb.append(this.articleTitle);

        sb.append("\n");

        sb.append(this.menu.getMenuName());

        sb.append("\n");

        for (Comment comment : comments) {

            sb.append(comment.getCommentContent());

            sb.append("\n");

        }

        return sb.toString();

    }

}


5.数据库 Mapper 文件 ArticleMapper.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.learn.dao.ArticleMapper">



    <resultMap id="article" type="com.learn.model.Article">

        <id column="article_id" property="articleId"/>

        <result column="article_path" property="articlePath"/>

        <result column="article_filename" property="articleFilename"/>

        <result column="article_title" property="articleTitle"/>

    </resultMap>



    <resultMap id="articleDetail" type="com.learn.model.Article">

        <id column="article_id" property="articleId"/>

        <result column="article_path" property="articlePath"/>

        <result column="article_filename" property="articleFilename"/>

        <result column="article_title" property="articleTitle"/>



        <association property="menu" javaType="com.learn.model.Menu">

            <id column="menu_id" property="menuId"/>

            <id column="menu_name" property="menuName"/>

        </association>



        <collection property="comments" javaType="java.util.ArrayList" ofType="com.learn.model.Comment"> <!--ofType集合中的类型-->

            <id column="comment_id" property="commentId"/>

            <result column="comment_content" property="commentContent"/>

            <result column="comment_article_id" property="commentArticleId"/>

        </collection>

    </resultMap>



    <select id="selectArticle" parameterType="java.lang.Integer" resultMap="article">

      SELECT article_id, article_path, article_filename, article_title FROM blog_article WHERE article_id = #{articleId}

    </select>



    <select id="selectArticleDetail" parameterType="java.lang.Integer" resultMap="articleDetail">

        SELECT article_id, article_path, article_filename, article_title, comment_id, comment_content, comment_article_id, menu_id, menu_name FROM blog_article INNER JOIN blog_comment ON(article_id=comment_article_id) INNER JOIN blog_menu ON(article_menuid=menu_id) WHERE article_id=#{articleId}

    </select>



</mapper>


6.与 Mapper 文件对应的接口 ArticleMapper.java

package com.learn.dao;



import com.learn.model.Article;



public interface ArticleMapper {



    Article selectArticle(int articleId);

    Article selectArticleDetail(int articleId);

}


7.包含 ArticleMapper 的业务层

package com.learn.service;



import com.learn.dao.ArticleMapper;

import com.learn.model.Article;



public class ArticleService {



    private ArticleMapper articleMapper;



    public ArticleMapper getArticleMapper() {

        return articleMapper;

    }



    public void setArticleMapper(ArticleMapper articleMapper) {

        this.articleMapper = articleMapper;

    }



    public Article selectArticle(int articleId) {

        return articleMapper.selectArticle(articleId);

    }



}


8. Spring 完整配置文件

<?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"

       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">



    <!--<context:property-placeholder location="classpath:connection.properties"/>-->

    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

        <property name="location" value="connection.properties"/>

    </bean>



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="${driver}"/>

        <property name="url" value="${url}"/>

        <property name="username" value="${username}"/>

        <property name="password" value="${password}"/>

    </bean>



    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

    </bean>



    <bean id="articleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

        <property name="mapperInterface" value="com.learn.dao.ArticleMapper"/>

        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>

    </bean>



    <bean id="articleService" class="com.learn.service.ArticleService">

        <property name="articleMapper" ref="articleMapper"/>

    </bean>



</beans>


9.测试

package com.learn.mybatis;



import com.learn.model.Article;

import com.learn.service.ArticleService;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class SpringMain {



    public static void main(String[] args) {

        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");

        ArticleService articleService = (ArticleService) applicationContext.getBean("articleService");

        Article article = articleService.selectArticle(28);

        System.out.println(article.getArticleTitle());

    }

}

 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情