SSM集成(一):Mybatis3測試

Spring4+Mybatis3+SpringMVC(基於註解)整合步聚: html

一)Mybatis3測試; java

二)Mybatis3+Spring4整合; mysql

三)Mybatis3+Spring4+SpringMVC整合; web

 

1、新建maven web項目
參考maven項目文章--Maven新建Web項目  spring

2、添加pom.xml依賴包 sql

 

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

  <modelVersion>4.0.0</modelVersion> json

 

  <groupId>com.ssm.demo1</groupId> api

  <artifactId>ssmdemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>ssmdemo</name>

  <url>http://maven.apache.org</url>

 

 <properties> 

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- spring版本號 --> 

        <spring.version>4.0.2.RELEASE</spring.version> 

        <!-- mybatis版本號 --> 

        <mybatis.version>3.2.6</mybatis.version> 

        <!-- log4j日誌文件管理包版本 --> 

        <slf4j.version>1.7.7</slf4j.version> 

        <log4j.version>1.2.12</log4j.version> 

    </properties> 

 

    <dependencies> 

        <dependency> 

            <groupId>junit</groupId> 

            <artifactId>junit</artifactId> 

            <version>4.11</version> 

<!-- 表示開發的時候引入,發佈的時候不會加載此包 --> 

<scope>test</scope> 

</dependency> 

<!-- spring核心包 --> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-core</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-web</artifactId> 

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

</dependency> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-oxm</artifactId> 

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

</dependency> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-tx</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-jdbc</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-webmvc</artifactId> 

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

</dependency> 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-aop</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-context-support</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.springframework</groupId> 

<artifactId>spring-test</artifactId> 

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

</dependency> 

<!-- mybatis核心包 --> 

<dependency> 

<groupId>org.mybatis</groupId> 

<artifactId>mybatis</artifactId> 

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

</dependency> 

<!-- mybatis/spring --> 

<dependency> 

<groupId>org.mybatis</groupId> 

<artifactId>mybatis-spring</artifactId> 

<version>1.2.2</version> 

</dependency> 

<!-- 導入java ee jar --> 

<dependency> 

<groupId>javax</groupId> 

<artifactId>javaee-api</artifactId> 

<version>7.0</version> 

</dependency> 

<!-- 導入Mysql數據庫連接jar --> 

<dependency> 

<groupId>mysql</groupId> 

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

<version>5.1.30</version> 

</dependency> 

<!-- 導入dbcpjar包,用來在applicationContext.xml中配置數據庫 --> 

<dependency> 

<groupId>commons-dbcp</groupId> 

<artifactId>commons-dbcp</artifactId> 

<version>1.2.2</version> 

</dependency> 

<!-- JSTL標籤類 --> 

<dependency> 

<groupId>jstl</groupId> 

<artifactId>jstl</artifactId> 

<version>1.2</version> 

</dependency> 

<!-- 日誌文件管理包 --> 

<!-- log start --> 

<dependency> 

<groupId>log4j</groupId> 

<artifactId>log4j</artifactId> 

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

</dependency> 

<!-- 格式化對象,方便輸出日誌 --> 

<dependency> 

<groupId>com.alibaba</groupId> 

<artifactId>fastjson</artifactId> 

<version>1.1.41</version> 

</dependency> 

 

 

<dependency> 

<groupId>org.slf4j</groupId> 

<artifactId>slf4j-api</artifactId> 

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

</dependency> 

 

<dependency> 

<groupId>org.slf4j</groupId> 

<artifactId>slf4j-log4j12</artifactId> 

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

</dependency> 

<!-- log end --> 

<!-- 映入JSON --> 

<dependency> 

<groupId>org.codehaus.jackson</groupId> 

<artifactId>jackson-mapper-asl</artifactId> 

<version>1.9.13</version> 

</dependency> 

<!-- 上傳組件包 --> 

<dependency> 

<groupId>commons-fileupload</groupId> 

<artifactId>commons-fileupload</artifactId> 

<version>1.3.1</version> 

</dependency> 

<dependency> 

<groupId>commons-io</groupId> 

<artifactId>commons-io</artifactId> 

<version>2.4</version> 

</dependency> 

<dependency> 

<groupId>commons-codec</groupId> 

<artifactId>commons-codec</artifactId> 

<version>1.9</version> 

</dependency>    

    </dependencies> 

   

    <build>

       <plugins>

           <plugin>

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

              <version>3.0</version>

              <configuration>

              <source>1.7</source>

              <target>1.7</target>

              </configuration>

              </plugin>

       </plugins>

    </build>

</project>

 

clear下項目,就會自動從網上下載依賴包,本地maven倉庫沒有滴

 

3、在src/main/java/目錄下新建以下包名
     com.ssm.demo2                             //

com.ssm.demo2.controller              //存放@controller 註解的類

  com.ssm.demo2.mapper                //mybatis接口類與mapper(*.xml)

  com.ssm.demo2.po                        //實體類

  com.ssm.demo2.service                  //業務類

 

4、在src/main/resources目錄新建以下包名

   mybatis                 //存放mybatis的配置文件,在整合後,此文件下將無效

   spring              //存放spring的配置文件

 

5、在src/main/resources目錄新建

      db.properties                   //數據庫鏈接信息配置

      log4j.properties               //日誌打印配置

 

6、db.properties添加內容以下:

jdbc.user=root

jdbc.password=root

jdbc.driverClass=com.mysql.jdbc.Driver

jdbc.jdbcUrl=jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=utf8

jdbc.initPoolSize=5

jdbc.maxPoolSize=10

 

註明:」 ?useUnicode=true&characterEncoding=utf8」設置編碼爲utf-8

如不配置,有時數據庫編碼沒配好,就可能出現查詢中文,不出結果的狀況, 以防萬一配上!

 

7、Log4j.properties添加內容以下:

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

註明:在正式發佈時,把debug改爲info,該文件放在classpath目錄下便可,框架會自動加載,

無須手工來加載!

項目結構以下:

 

8、新建數據庫 用Navicat for mysql 工具

 

 

8.1)、新建user數據表,結構以下

 

自行輸入幾條測試數據!

 

這樣數據庫就建好了!

 

9、在scr/main/resources中的mybatis目錄下

新建mybatis_cofig.xml文件並添加以下 內容:

注:在與spring整合後,該文件下的配置將交由spring管理!

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

      <!-- 加載數據源配置 -->

      <properties resource="db.properties" />

      <!-- 別名 -->

      <typeAliases>

            <typeAlias alias="User" type="com.ssm.demo2.po.User"/>

      </typeAliases>

 

      <!--配置jdbc -->

      <environments default="development">

           <environment id="development">

                 <transactionManager type="JDBC" />

                 <dataSource type="POOLED">

                      <property name="driver" value="${jdbc.driverClass}"/>

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

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

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

                 </dataSource>

           </environment>

      </environments>

</configuration>

 

10、接下來寫po實體類與mapper文件

 

po包中新建User.java:

public class User {

   private int id;

   private String username;

   private Date password;

   private char sex;

   private String address;

   setter/getter方法省略

}

11、新建mapper接口類及映射文件(採用註解和代理接口方式,原始接口的可查看其餘文章)

新建映射文件

src/main/resources/mybatis包下新建 UserMapper.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.ssm.demo2.mapper.UserMapper">

      <!--查找記錄  -->

      <select id="findUserList" parameterType="java.lang.String" resultType="User">

           select * from user where username like "%"#{value}"%"

      </select>

</mapper>

 

mapper包下新建UserMapper.java接口文件

內容以下:

public interface UserMapper {

      public List<User>findUserList(String name) throws Exception;

}

 

12、配置映射文件(註冊mapper

mybatis_config.xml中添加以下內容:

<mappers>

        <!-- 加載mapper -->

        <mapper resource="mybatis/UserMapper.xml" />      

</mappers>

 

十3、添加測試方法

src/test/main下新建com.ssm.demo2包並新鍵 MybatisTest.java文件

內容:

public class MybatisTest {

 

   private SqlSessionFactory factory;

   {

        String config ="mybatis/mybatis_config.xml";

        InputStream in;

        try {

              in = Resources.getResourceAsStream(config);

              factory=new SqlSessionFactoryBuilder().build(in);

        } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

        }

   }

   @Test

   public void test() throws Exception {

        System.out.println(factory);

        SqlSession session=factory.openSession();

        UserMapper usero=session.getMapper(UserMapper.class);

        List<User>users=usero.findUserList("www");

        System.out.println(users.size());

   }

}

 

運行並獲得打印結果

 

上面有顯示,查詢的sql語句!如在數據中有結果,查詢無數據,可運行上面的sql來排錯!

 

十4、把在mybatis_config.xml中手工註冊的mapper映射,改爲,自動掃描式的

mybatis_config.xml中的<mappers>標籤下添加以下內容

<!--

                 註解的方式

                 自動掃描mapper包下的全部文件, 加載mapper

                 可是mapper.xml必須與接口名字同樣

            -->

           <package name="com.ssm.demo2.mapper" />

 

注:這樣配置必須知足二個條件就是mapper接口類與mapper映射文件(*.xml)要同名,並在同一個包下!

resourcesmybatis目錄下的UserMapper.xml複製至com.ssm.demo2.mapper包下

並在mybatis_config.xml中註釋掉

<!-- 加載mapper -->

<!-- <mapper resource="mybatis/UserMapper.xml" /> -->

 

結構以下:

 

其餘的文件無須改動,再運行,也ok!

 

如今SSM整合的第一個框架已搭建好!

有關詳細的mybatis請自行查閱相關文章!此文章只是針對整合!

 

下面在引基礎上,整合spring框架,最後再整合springmvc!!



相關文章
相關標籤/搜索