乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      SSM 框架 Maven項(xiàng)目整合實(shí)例

       Levy_X 2017-10-10

      這里寫圖片描述


      我是根據(jù)這篇博客自己學(xué)習(xí)這個(gè)實(shí)例的:AndyLizh的專欄 SSM框架——詳細(xì)整合教程(Spring SpringMVC MyBatis)
      上面的這篇博客里有怎么搭建 Maven 環(huán)境,根據(jù)數(shù)據(jù)表自動(dòng)生成 Mybatis 的Dao層,Service層,Maping xml文件等教程。

      下面是我的實(shí)例實(shí)現(xiàn)的詳細(xì)步驟、源代碼和一些筆記、遇到的問題

      版本信息

      Eclipse版本 Neon.1a Release (4.6.1) apache-maven-3.3.9 數(shù)據(jù)庫(kù) MySQL5.7.12
      • 1
      • 2
      • 3

      maven 配置的中央倉(cāng)庫(kù)阿里云地址

      setting.xml

      . . <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> . .
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12

      完整項(xiàng)目結(jié)構(gòu)

      這里寫圖片描述

      SSM項(xiàng)目創(chuàng)建過程

      源代碼下載

      1.新建一個(gè)Maven Project

      這里寫圖片描述

      建好之后會(huì)提示下面的錯(cuò)誤:

      The superclass 'javax.servlet.http.HttpServlet' was not found on the Java Build Path index.jsp
      • 1

      在項(xiàng)目右鍵 Java Build Path 添加 Tomcat 服務(wù)器的依賴

      這里寫圖片描述

      修改 JRE 版本, Edit 更改為 Eclipse 工作空間默認(rèn)的 JRE 版本,我的是1.8

      之后還要在pom.xml文件中聲明 jre 的版本(配置pom.xml文件時(shí)給出),和你Java Build Path中配置的版本要相同,否則 maven update project又會(huì)回到1.5版本,這個(gè)版本和項(xiàng)目目錄下.settings/org.eclipse.wst.common.project.facet.core.xml的配置有關(guān)

      ... <plugins> <!-- 修改maven默認(rèn)的JRE編譯版本 防止maven update project之后 把jre配置的成默認(rèn)的1.5 根據(jù)自己的情況更改為1.7或1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16

      這里寫圖片描述

      2.配置Maven Project的pom.xml 文件

      默認(rèn)的 pom.xml 內(nèi)容:

      pom.xml

      <project xmlns='http://maven./POM/4.0.0' xmlns:xsi='http://www./2001/XMLSchema-instance' xsi:schemaLocation='http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.jxust</groupId> <artifactId>SSM-Test2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SSM-Test2 Maven Webapp</name> <url>http://maven.</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>SSM-Test2</finalName> </build> </project>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21

      Maven Project 最重要的就是配置這個(gè) pom.xml 文件,我在引用別人的pom.xml文件時(shí),發(fā)生了很多問題,這和 maven 中央倉(cāng)庫(kù)的地址應(yīng)該也有關(guān)系。

      說到阿里云的maven中央倉(cāng)庫(kù)的jar包,在我的測(cè)試中,像Spring 和Mybatis 最新的jar包都還沒有,需要低一個(gè)版本才有

      在配置 pom.xml 過程中,遇到了的幾個(gè)問題:


      • jackjson

      2.0版本前叫org.codehaus.jackson ,2.0版本之后com.fasterxml.jackson ,配置方式不同
      <!-- 2.0版本之后引入JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.5</version> </dependency>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      <!-- 2.0版本之前 引入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6


      • mybatis-spring

      升級(jí) Spring 或者 Mybatis 的版本是,mybatis-spring 包的版本也要跟著升級(jí)
      spring 版本 4.0.2,Mybatis 的版本為3.2.6時(shí),mybatis-spring的jar包1.2.0就行
      spring 版本 4.3.3,Mybatis 的版本為3.4.0時(shí),mybatis-spring的jar包1.3.0才可以
      <!--mybatis spring 插件 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7


      • org.mybatis.spring.SqlSessionFactoryBean找不到

      配置了mybatis spring 版本的不對(duì)
      .


      Maven引入需要的SSM 框架jar包,修改后:
      pom.xml

      <project xmlns='http://maven./POM/4.0.0' xmlns:xsi='http://www./2001/XMLSchema-instance' xsi:schemaLocation='http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.jxust</groupId> <artifactId>SSM-Test2</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SSM-Test2 Maven Webapp</name> <url>http://maven.</url> <properties> <!-- spring版本號(hào) --> <spring.version>4.3.3.RELEASE</spring.version> <!-- mybatis版本號(hào) --> <mybatis.version>3.4.0</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!-- 表示開發(fā)的時(shí)候引入,發(fā)布的時(shí)候不會(huì)加載此包 --> <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.3.0</version> </dependency> <!-- 導(dǎo)入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 導(dǎo)入Mysql數(shù)據(jù)庫(kù)鏈接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <!-- 導(dǎo)入dbcp的jar包,用來在applicationContext.xml中配置數(shù)據(jù)庫(kù) --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL標(biāo)簽類 --> <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> <!-- 格式化對(duì)象,方便輸出日志 --> <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>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.5</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> <finalName>SSM-Test2</finalName> <plugins> <!-- 修改maven默認(rèn)的JRE編譯版本 防止maven update project之后 把jre配置的成默認(rèn)的1.5 根據(jù)自己的情況更改為1.7或1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
      • 139
      • 140
      • 141
      • 142
      • 143
      • 144
      • 145
      • 146
      • 147
      • 148
      • 149
      • 150
      • 151
      • 152
      • 153
      • 154
      • 155
      • 156
      • 157
      • 158
      • 159
      • 160
      • 161
      • 162
      • 163
      • 164
      • 165
      • 166
      • 167
      • 168
      • 169
      • 170
      • 171
      • 172
      • 173
      • 174
      • 175
      • 176
      • 177
      • 178
      • 179
      • 180
      • 181
      • 182
      • 183
      • 184
      • 185
      • 186
      • 187

      有些jar包并不一定會(huì)用到

      之后保存,右擊項(xiàng)目-maven-update project,如果出現(xiàn)以下的異常

      JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfied. JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer. SSM-Test2
      • 1
      • 2

      這是web.xml文件頭信息配置的不正確,原來的2.3,不符合要求。

      解決步驟1:web.xml頭信息替換為下面的代碼:

      web.xml

      <?xml version='1.0' encoding='UTF-8'?> <web-app xmlns:xsi='http://www./2001/XMLSchema-instance' xmlns='http://java./xml/ns/javaee' xsi:schemaLocation='http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_3_0.xsd' id='WebApp_ID' version='3.0'> </web-app>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

      解決步驟2:關(guān)閉Eclipse,修改項(xiàng)目目錄下.settings/org.eclipse.wst.common.project.facet.core.xml的配置

      <installed facet='jst.web' version='2.3'/>
      • 1

      改為

      <installed facet='jst.web' version='3.0'/>
      • 1

      重啟Eclipse,右擊項(xiàng)目-maven-update project(不行,就先移除項(xiàng)目,導(dǎo)入)

      具體步驟參考:http://www.cnblogs.com/jebeljebel/p/4421098.html

      項(xiàng)目結(jié)構(gòu)為:
      這里寫圖片描述

      之后可以在 Java Build Path 中查看自己 maven 下載的jar包。

      這里寫圖片描述

      3.Spring與MyBatis的整合

      這里寫圖片描述

      3.1數(shù)據(jù)庫(kù)連接配置文件

      jdbc.properties

      driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/db_ssm username=root password=root initialSize=0 maxActive=20 maxIdle=20 minIdle=1 maxWait=60000
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

      注意字符后面不要帶空格,特別是當(dāng)你復(fù)制別人的代碼時(shí),例如 ‘root ‘,我就是犯了這個(gè)錯(cuò)誤

      3.2 Spring文件中配置mybatis

      包的路徑,在創(chuàng)建之后,記得改為自己的路徑

      spring-mybatis.xml

      <?xml version='1.0' encoding='UTF-8'?> <beans xmlns='http://www./schema/beans' xmlns:xsi='http://www./2001/XMLSchema-instance' xmlns:context='http://www./schema/context' xmlns:mvc='http://www./schema/mvc' xmlns:tx='http://www./schema/tx' xsi:schemaLocation='http://www./schema/beans http://www./schema/beans/spring-beans-4.2.xsd http://www./schema/context http://www./schema/context/spring-context-4.3.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc-4.3.xsd http://www./schema/tx http://www./schema/tx/spring-tx-4.3.xsd'> <!-- 自動(dòng)掃描 --> <context:component-scan base-package='com.jxust.ssm' > <!-- 不掃描@Controller注解的類--> <context:exclude-filter type='annotation' expression='org.springframework.stereotype.Controller'/> </context:component-scan> <!-- 引入配置文件 --> <bean id='propertyConfigurer' class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'> <property name='location' value='classpath:jdbc.properties' /> </bean> <bean id='dataSource' class='org.apache.commons.dbcp.BasicDataSource' destroy-method='close'> <property name='driverClassName' value='${driver}' /> <property name='url' value='${url}' /> <property name='username' value='${username}' /> <property name='password' value='${password}' /> <!-- 初始化連接大小 --> <property name='initialSize' value='${initialSize}'></property> <!-- 連接池最大數(shù)量 --> <property name='maxActive' value='${maxActive}'></property> <!-- 連接池最大空閑 --> <property name='maxIdle' value='${maxIdle}'></property> <!-- 連接池最小空閑 --> <property name='minIdle' value='${minIdle}'></property> <!-- 獲取連接最大等待時(shí)間 --> <property name='maxWait' value='${maxWait}'></property> </bean> <!-- spring和MyBatis整合,不需要mybatis的配置映射文件 --> <bean id='sqlSessionFactory' class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='dataSource' /> <!-- 自動(dòng)掃描mapping.xml文件 --> <property name='mapperLocations' value='classpath:com/jxust/ssm/mapping/*.xml'></property> </bean> <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫dao的實(shí)現(xiàn)類--> <bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <property name='basePackage' value='com.jxust.ssm.dao' /> <property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'></property> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id='transactionManager' class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='dataSource' /> </bean> <tx:annotation-driven transaction-manager='transactionManager'/> <!-- 在這里我有一個(gè)疑惑,原文博客這里并沒有配置事務(wù)注解,我測(cè)試了向數(shù)據(jù)庫(kù)添加數(shù)據(jù),也能成功,(我在寫Hibernate是需要配置事務(wù)注解的) 不知道是不是,service實(shí)現(xiàn)類,bean 使用了注解的方式,還是其他什么的 這里我把事務(wù)注解加上 spring、mybatis 配置方式 可以看這篇博客:http://blog.csdn.net/qh_java/article/details/51601139 --> </beans>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69

      3.3 log4j 的配置

      通過日志來輸出各種調(diào)試信息

      log4j.properties

      #定義LOG輸出級(jí)別 log4j.rootLogger=INFO,Console,File #定義日志輸出目的地為控制臺(tái) log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以靈活地指定日志輸出格式,下面一行是指定具體的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到達(dá)指定尺寸的時(shí)候產(chǎn)生一個(gè)新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定輸出目錄 log4j.appender.File.File = logs/ssm.log #定義文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級(jí)別日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19

      3.4 創(chuàng)建 MySQL 數(shù)據(jù)庫(kù)和表

      建表語(yǔ)句和原始數(shù)據(jù)

      mysql> use db_ssm; Database changed mysql> CREATE TABLE tb_user( -> id int(11) NOT NULL AUTO_INCREMENT, -> user_name varchar(40) NOT NULL, -> password varchar(40) NOT NULL, -> age int(4) NOT NULL, -> PRIMARY KEY(id)) -> ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.33 sec) mysql> select * from tb_user; ---- ----------- ---------- ----- | id | user_name | password | age | ---- ----------- ---------- ----- | 1 | 李白 | 123454 | 23 | | 2 | 杜甫 | 234234 | 23 | ---- ----------- ---------- ----- 2 rows in set (0.00 sec)
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

      3.5 利用MyBatis Generator自動(dòng)創(chuàng)建代碼

      該工具可根據(jù)MySQL中的表(tb_user)自動(dòng)創(chuàng)建實(shí)體類、MyBatis映射文件以及Dao接口
      參考博文:http://blog.csdn.net/zhshulin/article/details/23912615

      這里寫圖片描述

      紅色部分為自動(dòng)生成的文件,UserMaping.xml里各種包路徑,要修改成自己的,尤其是namespace,要關(guān)聯(lián)為創(chuàng)建的UserDao(因?yàn)槭褂昧藙?dòng)態(tài)代理實(shí)現(xiàn))

      你可以自己去嘗試自動(dòng)創(chuàng)建,下面給出這3個(gè)文件(注釋是我加的)。

      User.java

      package com.jxust.ssm.pojo; /** * 用戶實(shí)體類 * 對(duì)應(yīng)數(shù)據(jù)表tb_user * @author Peng * @Date2016年12月10日下午10:30:16 */ public class User { private Integer id; private String userName; private String password; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return 'User [id=' id ', userName=' userName ', password=' password ', age=' age ']'; } }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53

      UserDao.java

      package com.jxust.ssm.dao; import com.jxust.ssm.pojo.User; /** * User類Dao層接口 * * 之前我們會(huì)在dao層自己手動(dòng)實(shí)現(xiàn)dao層然后自動(dòng)注入SqlSessionTemplate 實(shí)例 * 來調(diào)用具體的方法 比如 insert('','') selectOne('','') 等方法 * 其中第一個(gè)參數(shù)就是映射文件的地址: namespace id 而第二個(gè)參數(shù)就是傳遞的條件這樣mybatis * 就會(huì)按照我們傳遞的這兩個(gè)參數(shù)找到具體的映射文件進(jìn)行解析查詢。 * 而這里使用動(dòng)態(tài)代理就省去了我們實(shí)現(xiàn)dao接口的這一步驟,而是由spring提我們實(shí)現(xiàn)了 * @author Peng * @Date2016年12月10日下午10:31:27 */ public interface UserDao { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27

      自動(dòng)創(chuàng)建的文件中,有增刪改查的各種方法,動(dòng)態(tài)SQL等,如果你想了解詳細(xì)的使用方法:http://blog.csdn.net/peng_hong_fu/article/details/53235271

      UserMapper.xml

      <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE mapper PUBLIC '-////DTD Mapper 3.0//EN' 'http:///dtd/mybatis-3-mapper.dtd' > <!-- namespace的值就是dao接口的完整路徑,就這個(gè)demo而言namespace 就是userDao.java的完整路徑--> <mapper namespace='com.jxust.ssm.dao.UserDao'> <resultMap id='BaseResultMap' type='com.jxust.ssm.pojo.User'> <id column='id' property='id' jdbcType='INTEGER' /> <result column='user_name' property='userName' jdbcType='VARCHAR' /> <result column='password' property='password' jdbcType='VARCHAR' /> <result column='age' property='age' jdbcType='INTEGER' /> </resultMap> <sql id='Base_Column_List'> id, user_name, password, age </sql> <select id='selectByPrimaryKey' resultMap='BaseResultMap' parameterType='java.lang.Integer'> select <include refid='Base_Column_List' /> from tb_user where id = #{id,jdbcType=INTEGER} </select> <delete id='deleteByPrimaryKey' parameterType='java.lang.Integer'> delete from tb_user where id = #{id,jdbcType=INTEGER} </delete> <insert id='insert' parameterType='com.jxust.ssm.pojo.User'> insert into tb_user (id, user_name, password, age) values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) </insert> <insert id='insertSelective' parameterType='com.jxust.ssm.pojo.User'> insert into tb_user <trim prefix='(' suffix=')' suffixOverrides=','> <if test='id != null'> id, </if> <if test='userName != null'> user_name, </if> <if test='password != null'> password, </if> <if test='age != null'> age, </if> </trim> <trim prefix='values (' suffix=')' suffixOverrides=','> <if test='id != null'> #{id,jdbcType=INTEGER}, </if> <if test='userName != null'> #{userName,jdbcType=VARCHAR}, </if> <if test='password != null'> #{password,jdbcType=VARCHAR}, </if> <if test='age != null'> #{age,jdbcType=INTEGER}, </if> </trim> </insert> <update id='updateByPrimaryKeySelective' parameterType='com.jxust.ssm.pojo.User'> update tb_user <set> <if test='userName != null'> user_name = #{userName,jdbcType=VARCHAR}, </if> <if test='password != null'> password = #{password,jdbcType=VARCHAR}, </if> <if test='age != null'> age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id='updateByPrimaryKey' parameterType='com.jxust.ssm.pojo.User'> update tb_user set user_name = #{userName,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> </mapper>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96

      3.6 創(chuàng)建 Service層 接口和實(shí)現(xiàn)類

      這里寫圖片描述

      UserService.java

      package com.jxust.ssm.service; import com.jxust.ssm.pojo.User; /** * Service層接口 * @author Peng * @Date2016年12月11日下午7:05:05 */ public interface UserService { //根據(jù)id查找 public User getUserById(Integer userid); //添加一條數(shù)據(jù) public int insert(User user); }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16

      UserServiceImpl.java

      package com.jxust.ssm.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.jxust.ssm.dao.UserDao; import com.jxust.ssm.pojo.User; import com.jxust.ssm.service.UserService; /** * userService 接口的實(shí)現(xiàn)類 * * @author Peng * @Date2016年12月11日上午11:50:01 */ @Transactional @Service('userService') public class UserServiceImpl implements UserService { /** * 因?yàn)闆]有在spring的文件中顯式的聲明userService實(shí)現(xiàn)類,直接通過getBean得到 userService 會(huì)找不到 * 需要顯式配置 @Service ('userService'),指定bean的名稱 * 相當(dāng)與<bean id='userService' class='com.jxust.ssm.service.impl.UserServiceImpl'></bean> */ @Resource private UserDao userDao; public User getUserById(Integer userid) { return this.userDao.selectByPrimaryKey(userid); } @Override public int insert(User user) { return this.userDao.insert(user); } }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36

      3.7 Junit 測(cè)試Spring整合Mybatis

      這里寫圖片描述

      使用Junit測(cè)試,TestMyBatis為使用 spring 的測(cè)試方法,TestMyBatis2為不使用 spring 的普通方法

      TestMyBatis.java

      package com.test; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.fastjson.JSON; import com.jxust.ssm.pojo.User; import com.jxust.ssm.service.UserService; @RunWith(SpringJUnit4ClassRunner.class) // 表示繼承了SpringJUnit4ClassRunner類 @ContextConfiguration(locations = { 'classpath:spring-mybatis.xml' }) /** * 測(cè)試spring整合mybatis spring方式 * * @author Peng * @Date2016年12月11日上午11:52:56 */ public class TestMyBatis { private static Logger logger = Logger.getLogger(TestMyBatis.class); @Resource private UserService userService = null; /** * 測(cè)試查詢 */ @Test public void test1() { User user = userService.getUserById(2); logger.info('值:' user.getUserName()); logger.info(JSON.toJSONString(user)); } /** * 測(cè)試添加 */ @Test public void test2() { User user = new User(); user.setUserName('杜甫3'); user.setPassword('234234'); user.setAge(23); int count = userService.insert(user); logger.info('count:' count); } }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50

      TestMyBatis2.java

      package com.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jxust.ssm.pojo.User; import com.jxust.ssm.service.UserService; /** * 測(cè)試spring整合mybatis 普通方式 * @author Peng * @Date2016年12月11日上午11:52:11 */ public class TestMyBatis2 { private ApplicationContext ac = null; private UserService userService = null; @Before public void before() { ac = new ClassPathXmlApplicationContext('classpath:spring-mybatis.xml'); userService = (UserService) ac.getBean('userService'); } /** * 測(cè)試查詢 */ @Test public void test1() { User user = userService.getUserById(2); System.out.println(user.toString()); } /** * 測(cè)試添加 */ @Test public void test2() { User user = new User(); user.setUserName('杜甫'); user.setPassword('234234'); user.setAge(23); int count = userService.insert(user); System.out.println('插入' count '條數(shù)據(jù)成功'); } }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46

      測(cè)試結(jié)果:

      ... [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring [com.test.TestMyBatis] - 值:杜甫 [com.test.TestMyBatis] - {'age':23,'id':2,'password':'234234','userName':'杜甫'} [org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@55d56113: startup date [Sun Dec 11 20:23:43 CST 2016]; root of context hierarchy
      • 1
      • 2
      • 3
      • 4
      • 5

      正確輸入結(jié)果,則測(cè)試成功!

      項(xiàng)目結(jié)構(gòu)為:

      這里寫圖片描述
      這里寫圖片描述

      4.項(xiàng)目整合 SpringMVC

      SpringMVC 涉及到視圖了,需要配置web.xml文件和SpringMVC的配置文件了

      4.1配置web.xml

      web.xml

      <?xml version='1.0' encoding='UTF-8'?> <web-app xmlns:xsi='http://www./2001/XMLSchema-instance' xmlns='http://java./xml/ns/javaee' xsi:schemaLocation='http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_3_0.xsd' id='WebApp_ID' version='3.0'> <!-- 編碼過濾器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring監(jiān)聽器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Spring MVC 配置servlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48

      使用 Eclipse在 web.xml 文件中,可以使用 Alt /快速創(chuàng)建ContextLoaderListenerDispatcherServlet兩個(gè)過濾器配置

      這里寫圖片描述

      4.2 配置SpringMVC配置文件springmvc.xml

      這里寫圖片描述

      springmvc.xml

      <?xml version='1.0' encoding='UTF-8'?> <beans xmlns='http://www./schema/beans' xmlns:xsi='http://www./2001/XMLSchema-instance' xmlns:context='http://www./schema/context' xmlns:mvc='http://www./schema/mvc' xsi:schemaLocation='http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context-4.0.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc-4.0.xsd'> <!-- 自動(dòng)掃描該包 @controller注解的類--> <context:component-scan base-package='com.jxust.ssm.controller'/> <!-- 靜態(tài)資源處理 --> <mvc:default-servlet-handler/> <!--自動(dòng)注冊(cè) RequestMappingHandlerMapping、RequestMappingHandlerAdapter 等bean --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置--> <bean class='org.springframework.web.servlet.view.InternalResourceViewResolver'> <property name='prefix' value='/WEB-INF/views/' /> <property name='suffix' value='.jsp' /> </bean> </beans>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      spring配置文件和springmvc配置文件的兩個(gè)自動(dòng)掃描的路徑范圍,最好不要重復(fù),使用<context:exclude-filter/><context:include-filter/>指定不掃描和掃描的條件

      4.3 創(chuàng)建 SpringMVC 的Controller

      這里寫圖片描述

      UserController.java

      package com.jxust.ssm.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.jxust.ssm.pojo.User; import com.jxust.ssm.service.UserService; @Controller public class UserController { /** * 使用@Autowired也可以,@Autowired默認(rèn)按類型裝配 * @Resource 默認(rèn)按名稱裝配,當(dāng)找不到與名稱匹配的bean才會(huì)按類型裝配。 */ @Resource private UserService userService; /** * 測(cè)試查詢 * * @param id * @param model * @return */ @RequestMapping('/showUser') public String testtoshowUser(@RequestParam(value = 'id') Integer id, Model model) { System.out.println('id:' id); User user = userService.getUserById(id); model.addAttribute('user', user); return 'showUser'; } /** * 測(cè)試添加數(shù)據(jù) * * @param id * @param model * @return */ @RequestMapping('/insertUser') public String testinsertUser() { User user = new User(); user.setUserName('李清照'); user.setPassword('3232322'); user.setAge(22); int count = userService.insert(user); System.out.println('插入' count '條數(shù)據(jù)成功'); return 'showUser'; } }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54

      4.4 JSP頁(yè)面

      這里寫圖片描述

      index.jsp

      <%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www./TR/html4/loose.dtd'> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> <title>index.jsp</title> </head> <body> <a href='showUser?id=2'>ShowUser page</a><br> <a href='insertUser'>insertUser </a> </body> </html>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

      showUser.jsp

      <%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%> <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www./TR/html4/loose.dtd'> <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> <title>測(cè)試</title> </head> <body> id:${user.id} <br> userName: ${user.userName} <br> password: ${user.password} <br> age: ${user.age} <br> </body> </html>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      JSP頁(yè)面寫完,就可以發(fā)布了

      4.5 項(xiàng)目發(fā)布到Tomcat服務(wù)器上

      這里寫圖片描述

      源碼下載

      Maven 項(xiàng)目就是小,沒有jar包,只有 30k

      源代碼下載:http://download.csdn.net/detail/peng_hong_fu/9708357

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多