From c64564ce7a1c7576045b90c91f4c8614ffc9a0a0 Mon Sep 17 00:00:00 2001 From: sjq597 Date: Fri, 7 Aug 2015 00:36:09 +0800 Subject: [PATCH 01/10] =?UTF-8?q?Spring=E5=88=86=E6=94=AF=EF=BC=8C?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=AE=80=E5=8D=95=E7=9A=84Spring=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaPracticeCode.iml | 13 ++ pom.xml | 36 +++- .../java/com/learn/note/practice/App.java | 13 -- .../com/learn/note/practice/ioc/README.MD | 173 ++++++++++++++++++ .../practice/ioc/common/beans/UserBean.java | 44 +++++ src/main/resources/config.xml | 9 + .../java/com/learn/note/practice/AppTest.java | 38 ---- .../ioc/common/beans/UserBeanTest.java | 23 +++ 8 files changed, 292 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/com/learn/note/practice/App.java create mode 100644 src/main/java/com/learn/note/practice/ioc/README.MD create mode 100644 src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java create mode 100644 src/main/resources/config.xml delete mode 100644 src/test/java/com/learn/note/practice/AppTest.java create mode 100644 src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java diff --git a/JavaPracticeCode.iml b/JavaPracticeCode.iml index 8979886..14f21d4 100644 --- a/JavaPracticeCode.iml +++ b/JavaPracticeCode.iml @@ -6,10 +6,23 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 407125c..52b4984 100644 --- a/pom.xml +++ b/pom.xml @@ -14,12 +14,36 @@ UTF-8 - + - junit - junit - 3.8.1 - test + junit + junit + 4.10 + test - + + org.slf4j + slf4j-api + 1.7.5 + + + ch.qos.logback + logback-classic + 1.0.13 + + + ch.qos.logback + logback-core + 1.0.13 + + + + + + org.springframework + spring-context + 4.0.0.RELEASE + + + diff --git a/src/main/java/com/learn/note/practice/App.java b/src/main/java/com/learn/note/practice/App.java deleted file mode 100644 index 25057b4..0000000 --- a/src/main/java/com/learn/note/practice/App.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.learn.note.practice; - -/** - * Hello world! - * - */ -public class App -{ - public static void main( String[] args ) - { - System.out.println( "Hello World!" ); - } -} diff --git a/src/main/java/com/learn/note/practice/ioc/README.MD b/src/main/java/com/learn/note/practice/ioc/README.MD new file mode 100644 index 0000000..1e5d51b --- /dev/null +++ b/src/main/java/com/learn/note/practice/ioc/README.MD @@ -0,0 +1,173 @@ +#### Spring学习笔记 + +#### 第一课,依赖注入,也称IOC +java里反射是个非常强大的东西,Spring之所以能够实现也多亏java的反射机制 +先看一个简单的例子,来看看Spring是如何工作的。 + +#### 配置 +* 新建三个包`ioc.common.beans`,`resources`,在`beans`目录下新建一个`UserBean`类,内容如下 +```java +package com.learn.note.practice.ioc.common.beans; + +/** + * @author junqiangshen + * @version v1.0. + * @Time Created on 15-8-6. + */ +public class UserBean { + + private String userName; + private String userPassword; + + public UserBean() { + } + + public UserBean(String userName, String userPassword) { + this.userName = userName; + this.userPassword = userPassword; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserPassword() { + return userPassword; + } + + public void setUserPassword(String userPassword) { + this.userPassword = userPassword; + } + + @Override + public String toString() { + return "UserBean{" + + "userName='" + userName + '\'' + + ", userPassword='" + userPassword + '\'' + + '}'; + } +} +``` + +在`resources`目录下新建一个`config.xml`文件,内容如下 +```xml + + + + + + + + +``` +然后创建一个单元测试`UserBeanTest.java` +```java +package com.learn.note.practice.ioc.common.beans; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.*; + +public class UserBeanTest { + + @Test + public void testUserBeanSpring() { + ApplicationContext applicationContext = + new ClassPathXmlApplicationContext(new String[] {"config.xml"}); + // 取得一个实例 + UserBean user = (UserBean) applicationContext.getBean("userBean"); + System.out.println(user); + } +} + +``` +注意最终的目录结构是这样的 +``` +. +├── JavaPracticeCode.iml +├── pom.xml +├── README.MD +├── src +│   ├── main +│   │   ├── java +│   │   │   └── com +│   │   │   └── learn +│   │   │   └── note +│   │   │   └── practice +│   │   │   └── ioc +│   │   │   ├── common +│   │   │   │   └── beans +│   │   │   │   └── UserBean.java +│   │   │   └── README.MD +│   │   └── resources +│   │   └── config.xml +│   └── test +│   └── java +│   └── com +│   └── learn +│   └── note +│   └── practice +│   └── ioc +│   └── common +│   └── beans +│   └── UserBeanTest.java + +``` + +**NOTE:**关于在idea中把文件夹标注为制定文件夹根目录的方法,在文件夹上`右键->Mark Directory As` +具体有四个选项,分别是 +* `Sources Root` 源代码java文件根目录,本例中`src/main/java`文件夹就是默认的 +* `Test Sources Root` 测试代码根目录,本例中`test/java`目录是 +* `Resources Root`资源文件根目录,本例中的`resources`目录 +* `Excluded`编译的类`.class`文件根目录,本例中是`target`,对于所有`java`目录下的资源,编译时都会拷贝到对应的`Excluded`文件夹下 + +我们可以在`JavaPracticeCode.iml`文件中看到被标记的情况 +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` +注意这几行 +```xml + + + + + + +``` +以后如果发现单元测试无法创建或者运行,或者资源文件加载不上,自行加标记。 +单元测试的输出结果如下: +> 00:20:20.204 [main] INFO c.l.n.p.ioc.common.beans.UserBean - UserBean{userName='Fuck', userPassword='hehehe'} \ No newline at end of file diff --git a/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java b/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java new file mode 100644 index 0000000..3788eb8 --- /dev/null +++ b/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java @@ -0,0 +1,44 @@ +package com.learn.note.practice.ioc.common.beans; + +/** + * @author junqiangshen + * @version v1.0. + * @Time Created on 15-8-6. + */ +public class UserBean { + + private String userName; + private String userPassword; + + public UserBean() { + } + + public UserBean(String userName, String userPassword) { + this.userName = userName; + this.userPassword = userPassword; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserPassword() { + return userPassword; + } + + public void setUserPassword(String userPassword) { + this.userPassword = userPassword; + } + + @Override + public String toString() { + return "UserBean{" + + "userName='" + userName + '\'' + + ", userPassword='" + userPassword + '\'' + + '}'; + } +} diff --git a/src/main/resources/config.xml b/src/main/resources/config.xml new file mode 100644 index 0000000..fe147ad --- /dev/null +++ b/src/main/resources/config.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/src/test/java/com/learn/note/practice/AppTest.java b/src/test/java/com/learn/note/practice/AppTest.java deleted file mode 100644 index 51a391d..0000000 --- a/src/test/java/com/learn/note/practice/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.learn.note.practice; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -} diff --git a/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java b/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java new file mode 100644 index 0000000..f0246e2 --- /dev/null +++ b/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java @@ -0,0 +1,23 @@ +package com.learn.note.practice.ioc.common.beans; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.junit.Assert.*; + +public class UserBeanTest { + private static Logger logger = LoggerFactory.getLogger(UserBean.class); + + @Test + public void testUserBeanSpring() { + ApplicationContext applicationContext = + new ClassPathXmlApplicationContext(new String[] {"config.xml"}); + // 取得一个实例 + UserBean user = (UserBean) applicationContext.getBean("userBean"); + logger.info(user.toString()); + } + +} \ No newline at end of file From bd762acda8e792d518fb86f1c853dbc70dd1ed1e Mon Sep 17 00:00:00 2001 From: sjq597 Date: Sat, 8 Aug 2015 00:12:47 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=85=8D=E7=BD=AEsprin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JavaPracticeCode.iml | 10 +++++++++- README.MD | 12 ++++++++++++ .../note/practice/ioc/common/beans/UserBean.java | 1 + src/main/resources/config.xml | 4 ++-- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/JavaPracticeCode.iml b/JavaPracticeCode.iml index 14f21d4..890ea5f 100644 --- a/JavaPracticeCode.iml +++ b/JavaPracticeCode.iml @@ -1,5 +1,14 @@ + + + + + file://$MODULE_DIR$/src/main/resources/config.xml + + + + @@ -11,7 +20,6 @@ - diff --git a/README.MD b/README.MD index 71af03a..b1e1226 100644 --- a/README.MD +++ b/README.MD @@ -70,3 +70,15 @@ To push the current branch and set the remote as upstream, use ~/JavaPracticeCode git push origin master Everything up-to-date ``` +添加spring配置 +```xml + + + + + file://$MODULE_DIR$/src/main/resources/config.xml + + + + +``` \ No newline at end of file diff --git a/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java b/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java index 3788eb8..c9bb43c 100644 --- a/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java +++ b/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java @@ -14,6 +14,7 @@ public UserBean() { } public UserBean(String userName, String userPassword) { + this.userName = userName; this.userPassword = userPassword; } diff --git a/src/main/resources/config.xml b/src/main/resources/config.xml index fe147ad..0e91925 100644 --- a/src/main/resources/config.xml +++ b/src/main/resources/config.xml @@ -3,7 +3,7 @@ "http://www.springframework.org/dtd/spring-beans.dtd"> - - + + \ No newline at end of file From 8cca5411312d1dffb7e9cb6e26fa004161838ae4 Mon Sep 17 00:00:00 2001 From: sjq597 Date: Fri, 28 Aug 2015 00:10:54 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6=E4=B8=94=E5=B8=A6=E6=9C=89?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 + pom.xml | 171 +++++++--- spring-bbs-demo/README.MD | 291 ++++++++++++++++++ spring-bbs-demo/pom.xml | 97 ++++++ .../spring-bbs-demo.iml | 38 ++- .../java/com/springbbs/dao/LoginLogDao.java | 24 ++ .../main/java/com/springbbs/dao/UserDao.java | 52 ++++ .../java/com/springbbs/domain/LoginLog.java | 59 ++++ .../main/java/com/springbbs/domain/User.java | 79 +++++ .../com/springbbs/service/UserService.java | 43 +++ .../src/main/resources/applicationContext.xml | 51 +++ .../src/main/resources/jdbc.properties | 4 + .../main/resources/sql/user_table_init.sql | 26 ++ .../springbbs/service/UserServiceTest.java | 42 +++ .../src/main/webapp/WEB-INF/web.xml | 7 + spring-bbs-demo/src/main/webapp/index.jsp | 5 + spring-bbs-demo/target/MANIFEST.MF | 5 + .../target/classes/applicationContext.xml | 50 +++ .../com/springbbs/dao/LoginLogDao.class | Bin 0 -> 1228 bytes .../classes/com/springbbs/dao/UserDao$1.class | Bin 0 -> 1242 bytes .../classes/com/springbbs/dao/UserDao.class | Bin 0 -> 2137 bytes .../com/springbbs/domain/LoginLog.class | Bin 0 -> 1749 bytes .../classes/com/springbbs/domain/User.class | Bin 0 -> 2199 bytes .../com/springbbs/service/UserService.class | Bin 0 -> 1878 bytes .../target/classes/jdbc.properties | 4 + .../target/classes/sql/user_table_init.sql | 26 ++ .../springbbs/service/UserServiceTest.class | Bin 0 -> 1955 bytes 27 files changed, 1022 insertions(+), 58 deletions(-) create mode 100644 spring-bbs-demo/README.MD create mode 100644 spring-bbs-demo/pom.xml rename JavaPracticeCode.iml => spring-bbs-demo/spring-bbs-demo.iml (55%) create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/dao/LoginLogDao.java create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/dao/UserDao.java create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/domain/LoginLog.java create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/domain/User.java create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/service/UserService.java create mode 100644 spring-bbs-demo/src/main/resources/applicationContext.xml create mode 100644 spring-bbs-demo/src/main/resources/jdbc.properties create mode 100644 spring-bbs-demo/src/main/resources/sql/user_table_init.sql create mode 100644 spring-bbs-demo/src/main/test/java/com/springbbs/service/UserServiceTest.java create mode 100644 spring-bbs-demo/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-bbs-demo/src/main/webapp/index.jsp create mode 100644 spring-bbs-demo/target/MANIFEST.MF create mode 100644 spring-bbs-demo/target/classes/applicationContext.xml create mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class create mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class create mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class create mode 100644 spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class create mode 100644 spring-bbs-demo/target/classes/com/springbbs/domain/User.class create mode 100644 spring-bbs-demo/target/classes/com/springbbs/service/UserService.class create mode 100644 spring-bbs-demo/target/classes/jdbc.properties create mode 100644 spring-bbs-demo/target/classes/sql/user_table_init.sql create mode 100644 spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class diff --git a/.gitignore b/.gitignore index fb6851a..959c62b 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ # 忽略.idea文件夹下的项目信息文件 .idea + +# 忽略classes目录 +target/ + +# 忽略工程文件 +*.iml diff --git a/pom.xml b/pom.xml index 52b4984..ebf54d4 100644 --- a/pom.xml +++ b/pom.xml @@ -1,49 +1,126 @@ - 4.0.0 - - com.learn.note.practice - code - 1.0-SNAPSHOT - jar - - code - http://maven.apache.org - - - UTF-8 - - - - - junit - junit - 4.10 - test - - - org.slf4j - slf4j-api - 1.7.5 - - - ch.qos.logback - logback-classic - 1.0.13 - - - ch.qos.logback - logback-core - 1.0.13 - - - - - - org.springframework - spring-context - 4.0.0.RELEASE - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.learn.note.practice + code + 1.0-SNAPSHOT + + spring-bbs-demo + + pom + + code + http://maven.apache.org + + + UTF-8 + 4.11 + 1.7.7 + 1.0.13 + 4.0.0.RELEASE + 1.8.0 + 5.1.34 + 1.2.2 + + + + + + + + junit + junit + ${junit.version} + test + + + org.springframework + spring-test + ${spring.version} + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + ch.qos.logback + logback-core + ${logback.version} + + + + + org.springframework + spring-aop + ${spring.version} + + + org.springframework + spring-beans + ${spring.version} + + + org.springframework + spring-context + ${spring.version} + + + org.springframework + spring-core + ${spring.version} + + + org.springframework + spring-jdbc + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.springframework + spring-webmvc + ${spring.version} + + + org.springframework + spring-tx + ${spring.version} + + + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + + + mysql + mysql-connector-java + ${mysql.version} + + + + commons-dbcp + commons-dbcp + ${dhcp.version} + + + + + diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD new file mode 100644 index 0000000..e8521c8 --- /dev/null +++ b/spring-bbs-demo/README.MD @@ -0,0 +1,291 @@ +### 用spring实现一个论坛基本功能 + +#### 运行环境 +Linux:Ubun 14.04 64bit +IDE:IntelliJ IDEA 14.03 +JDK:1.7.40 +MySQL:5.5.44 +Tomcat:7.0.47 +​Maven:3.0.5 + +#### 具体步骤 +* 新建一个Webapp工程,名字就叫spring-bbs-demo +项目的目录结构如下: +``` +├── pom.xml +├── README.MD +├── spring-bbs-demo.iml +└── src + └── main + ├── resources + └── webapp + ├── index.jsp + └── WEB-INF + └── web.xml + +``` +* 在mysql里创建两张表,数据库名为`sampledb` +```sql +DROP DATABASE IF EXISTS sampledb; +CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8mb4; +USE sampledb; + +-- 创建用户表 +CREATE TABLE t_user ( + user_id INT AUTO_INCREMENT NOT NULL COMMENT '用户id', + user_name VARCHAR(30) NOT NULL DEFAULT '' COMMENT '用户名', + credits INT NOT NULL DEFAULT 0 COMMENT '论坛积分', + password VARCHAR(32) NOT NULL DEFAULT ''COMMENT '用户密码', + last_visit TIMESTAMP NOT NULL DEFAULT 0 COMMENT '最后访问时间', + last_ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '最后访问ip', + PRIMARY KEY (user_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户表'; + +-- 创建用户登陆日志表 +CREATE TABLE t_login_log ( + login_log_id INT AUTO_INCREMENT NOT NULL COMMENT '日志id', + user_id INT NOT NULL DEFAULT 0 COMMENT '用户id', + ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '访问ip', + login_datetime TIMESTAMP NOT NULL DEFAULT 0 COMMENT '访问时间', + PRIMARY KEY (login_log_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户登陆日志表'; + +INSERT INTO t_user (user_name,password) VALUES ('admin','123456'); +``` + +#### 最终项目目录结构 + +``` +├── pom.xml +├── README.MD +├── spring-bbs-demo.iml +└── src + └── main + ├── java + │   └── com + │   └── springbbs + │   ├── dao + │   ├── domain + │   └── service + ├── resources + │   └── sql + │   └── user_table_init.sql + └── webapp + ├── index.jsp + └── WEB-INF + └── web.xml + +``` +**NOTE:**`java`为代码根目录,如果不是,可以在`java`文件夹上右击`->Mark Directory As->Resources Root`。 +同理,`resources`为资源根目录,`webapp`为`web`根目录,`test`目录下的`java`文件夹为`TestR Resources Root`目录 + +#### 文件及文件夹相关函数 + +文件 | 解释 +-----| -------------- +applicationContext.xml | Spring容器配置文件 +domain | 领域对象(实体类)存放文件夹,往往拥有对应的数据库表,一般要实现`Serializable`接口,以便序列化 +dao | 访问实体类的接口,一般一个实体类都会有一个dao与之对应,里面有一些对应的方法 +jdbcTemplate | Spring对jdbc的简单封装,可以轻松完成大部分的数据库操作而不必频繁的重复对数据库的打开,获取连接,查询,关闭等操作 +jdbcTemplate#query() | `query(String sql,Object[] args,RowCallbackHandler rch)`.第一个参数不解释了;第二个是占位符(?)对应的参数数据;第三个是查询结果的处理回调接口,该回调接口有一个方法`processRow(ResultSet resultSet)`负责将查询结果从`ResultSet`装载到类似于实例类对象的实例中。一般都是使用匿名内部类的方式来调用`RowCallbackHandler`接口 + +**NOTE:**在DAO文件中写的`sql`语句比较长,多行衔接要注意空格,不然会连在一起,具体小技巧是每一行最后加个空格。 + +### Spring中配置 +以上两个DAO实现类中并没有打开/和释放`Connection`,到底如何访问数据库呢?答案是`jdbc`被`spring`封装起来了,`JdbcTemplate`需要一个`dataSource`,从数据源中获取或返回连接。所以在`UserDao`和`LoginLogDao`中都提供了一个带`@Autowired`注解的`jdbcTemplate`对象。 +所以我们需要先申明一个数据源,然后再定义一个`JdbcTemplate Bean`,通过`Spring`的容器上下文自动绑定机制进行`Bean`的注入。配置文件在`Resources`文件夹下,名字叫`applicationContext.xml`,内容如下: +```xml + + + + + + + + + + + p:driverClassName="${jdbc.driver}" + p:url="${jdbc.url}" + p:username="${jdbc.username}" + p:password="${jdbc.password}"/> + + + + +``` +##### 配置说明 +`jdbc.properties`文件为mysql配置文件,内容如下: +> +jdbc.driver=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/sampledb +jdbc.username=root +jdbc.password=root0724 + +在配置文件中加载mysql配置文件,在定义数据源时需要使用到配置文件中的信息 + +#### 业务层 +在这个登陆实例中,仅有一个业务类,即`UserService`,它负责将持久层的`UserDao`和`LoginDao`组织起来完成用户/密码认证、登陆日志记录等操作。`loginSucess`将两个`DAO`组织起来共同完成一个事务性工作:更新两个表,虽然没有事务操作的影子,但是通过`Spring`事务配置即可。 +关于几个注解的解释: +注解 | 作用 +-----|------ +`@Service` | 将`UserService`标注为一个服务层的`Bean` +`@Autowired` | 注入`userDao`和`loginLogDao`这两个`DAO`层的`Bean` + +#### 在Spring中装配Service +我们必须告诉`Spring`哪些业务类需要工作于事务环境下及事务的规则等内容,以便`Spring`根据这些信息自动为目标业务类添加事务管理功能。对`applicationContext.xml`更改: +```xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + p:driverClassName="${jdbc.driver}" + p:url="${jdbc.url}" + p:username="${jdbc.username}" + p:password="${jdbc.password}"/> + + + + +``` +**NOTE:** 配置文件解释 +* 命名空间 +```xml + +xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" +``` + * Service注解扫描目录 + ```xml + + ``` +* 事务管理器 +```xml + +``` +* aop事务增强 +关于aop(面向切片的编程后面会细讲,目前可以理解为拦截和代理即可),`execution(* com.springbbs.service.. *(..))`的意思就是任意返回值的service包及子包下的任何参数的任何方法都切入进行事务管理。 +```xml + + + + + + + + + + + +``` + +#### 单元测试 +依赖 +```xml + + junit + junit + test + + + org.springframework + spring-test + test + +``` +创建单元测试的方法:光标放在在需要测试的类名字上(本例中是`UserService`),按住`Ctrl+Shift+T`,`Junit4`单元测试,勾选3个方法即可。 +测试类`UserServiceTest`代码如下: +```java +package com.springbbs.service; + +import com.springbbs.domain.User; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Date; + +@RunWith(SpringJUnit4ClassRunner.class) // 基于JUnit4的Spring测试框架 +@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //启动Spring容器 +public class UserServiceTest { + + @Autowired + private UserService userService; + + @Test + public void testHasMatchUser() throws Exception { + boolean b1 = userService.hasMatchUser("admin", "123456"); + boolean b2 = userService.hasMatchUser("admin", "1111"); + + Assert.assertEquals(true, b1); + Assert.assertEquals(false, b2); + } + + @Test + public void testFindUserByUserName() throws Exception { + User user = userService.findUserByUserName("admin"); + Assert.assertEquals("admin", user.getUserName()); + } + + @Test + public void testLoginSucess() throws Exception { + User user = userService.findUserByUserName("admin"); + user.setLastIp("127.0.0.1"); + user.setLastVistit(new Date()); + userService.loginSucess(user); + } +} +``` +**NOTE:**`Spring`测试框架可以和`Junit4`整合,通过`Junit4`的`@RunWith`注解指定`SpringJUnit4ClassRunner.class`的测试运行器,该运行器是Spring提供的,可以将`Spring`容器和`Junit4`测试框架整合。`@ContextConfiguration`也是`Spring`提供的注解,它用于制定`Spring`的配置文件。 +这里需要注意的是,因为我的resources文件夹被指定为资源根目录,所以使用的 `classpath`路径来加载,即最终在类的根目录下。 +在`UserServiceTest`上右键->`Run UserServiceTest`即可运行单元测试。最后取数据库中查询登陆日志可以发现成功插入一条记录。 \ No newline at end of file diff --git a/spring-bbs-demo/pom.xml b/spring-bbs-demo/pom.xml new file mode 100644 index 0000000..f565980 --- /dev/null +++ b/spring-bbs-demo/pom.xml @@ -0,0 +1,97 @@ + + + code + com.learn.note.practice + 1.0-SNAPSHOT + + 4.0.0 + spring-bbs-demo + war + spring-bbs-demo Maven Webapp + http://maven.apache.org + + + + + junit + junit + test + + + org.springframework + spring-test + test + + + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + ch.qos.logback + logback-core + + + + + org.springframework + spring-aop + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + org.springframework + spring-jdbc + + + org.springframework + spring-tx + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + + + + org.aspectj + aspectjweaver + ${aspectj.version} + + + + + mysql + mysql-connector-java + + + + commons-dbcp + commons-dbcp + + + + + spring-bbs-demo + + diff --git a/JavaPracticeCode.iml b/spring-bbs-demo/spring-bbs-demo.iml similarity index 55% rename from JavaPracticeCode.iml rename to spring-bbs-demo/spring-bbs-demo.iml index 890ea5f..08d9772 100644 --- a/JavaPracticeCode.iml +++ b/spring-bbs-demo/spring-bbs-demo.iml @@ -1,11 +1,18 @@ - + - - file://$MODULE_DIR$/src/main/resources/config.xml - + + + + + + + + + + @@ -13,24 +20,33 @@ - - + + - - - + + + + - + + - + + + + + + + + \ No newline at end of file diff --git a/spring-bbs-demo/src/main/java/com/springbbs/dao/LoginLogDao.java b/spring-bbs-demo/src/main/java/com/springbbs/dao/LoginLogDao.java new file mode 100644 index 0000000..1042021 --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/dao/LoginLogDao.java @@ -0,0 +1,24 @@ +package com.springbbs.dao; + +import com.springbbs.domain.LoginLog; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +/** + * @Description JavaPracticeCode + * Created by junqiangshen on 15-8-26. + */ +@Repository +public class LoginLogDao { + + @Autowired + private JdbcTemplate jdbcTemplate; + + public void insertLoginLog(LoginLog loginLog) { + String sqlStr = "INSERT INTO t_login_log(user_id,ip,login_datetime) " + + "VALUES(?,?,?)"; + Object[] args = {loginLog.getUserId(), loginLog.getIp(), loginLog.getLoginDate()}; + jdbcTemplate.update(sqlStr, args); + } +} diff --git a/spring-bbs-demo/src/main/java/com/springbbs/dao/UserDao.java b/spring-bbs-demo/src/main/java/com/springbbs/dao/UserDao.java new file mode 100644 index 0000000..78091d6 --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/dao/UserDao.java @@ -0,0 +1,52 @@ +package com.springbbs.dao; + +import com.springbbs.domain.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowCallbackHandler; +import org.springframework.stereotype.Repository; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * @Description 访问User的类 + * Created by junqiangshen on 15-8-26. + */ +@Repository //1.通过Spring注解定义一个DAO +public class UserDao { + + @Autowired //2.自动注入jdbcTemplate的Bean + private JdbcTemplate jdbcTemplate; + + public int getMatchCount(String userName, String password) { + String sqlStr = "SELECT count(*) FROM t_user " + + "WHERE user_name=? and password=?"; + return jdbcTemplate.queryForObject(sqlStr, new Object[]{userName, password}, Integer.class); + } + + public User findUserByUserName(final String userName) { + String sqlStr = "SELECT user_id,user_name,credits " + + "FROM t_user WHERE user_name=?"; + final User user = new User(); + jdbcTemplate.query(sqlStr, new Object[]{userName}, + new RowCallbackHandler() { + @Override + public void processRow(ResultSet resultSet) throws SQLException { + user.setUserId(resultSet.getInt("user_id")); + user.setUserName(userName); + user.setCredits(resultSet.getInt("credits")); + } + } + ); + + return user; + } + + public void updateLoginInfo(User user) { + String sqlStr = "UPDATE t_user SET last_visit=?,last_ip=?,credits=? " + + "WHERE user_id=?"; + jdbcTemplate.update(sqlStr, new Object[]{user.getLastVistit(), + user.getLastIp(), user.getCredits(), user.getUserId()}); + } +} diff --git a/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginLog.java b/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginLog.java new file mode 100644 index 0000000..844049c --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginLog.java @@ -0,0 +1,59 @@ +package com.springbbs.domain; + +import java.io.Serializable; +import java.util.Date; + +/** + * @Description 登陆日志实体类 + * Created by junqiangshen on 15-8-26. + */ +public class LoginLog implements Serializable { + private static final long serialVersionUID = 1141217463557201669L; + + private int loginLogId; + private int userId; + private String ip; + private Date loginDate; + + public int getLoginLogId() { + return loginLogId; + } + + public void setLoginLogId(int loginLogId) { + this.loginLogId = loginLogId; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public Date getLoginDate() { + return loginDate; + } + + public void setLoginDate(Date loginDate) { + this.loginDate = loginDate; + } + + @Override + public String toString() { + return "LoginLog{" + + "loginLogId=" + loginLogId + + ", userId=" + userId + + ", ip='" + ip + '\'' + + ", loginDate=" + loginDate + + '}'; + } +} diff --git a/spring-bbs-demo/src/main/java/com/springbbs/domain/User.java b/spring-bbs-demo/src/main/java/com/springbbs/domain/User.java new file mode 100644 index 0000000..cb072b9 --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/domain/User.java @@ -0,0 +1,79 @@ +package com.springbbs.domain; + +import java.io.Serializable; +import java.util.Date; + +/** + * @Description User实体类 + * Created by junqiangshen on 15-8-26. + */ +public class User implements Serializable { + private static final long serialVersionUID = 3628238945647868396L; + + private int userId; + private String userName; + private String password; + private int credits; + private String lastIp; + private Date lastVistit; + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public int getCredits() { + return credits; + } + + public void setCredits(int credits) { + this.credits = credits; + } + + public String getLastIp() { + return lastIp; + } + + public void setLastIp(String lastIp) { + this.lastIp = lastIp; + } + + public Date getLastVistit() { + return lastVistit; + } + + public void setLastVistit(Date lastVistit) { + this.lastVistit = lastVistit; + } + + @Override + public String toString() { + return "User{" + + "userId=" + userId + + ", userName='" + userName + '\'' + + ", password='" + password + '\'' + + ", credits=" + credits + + ", lastIp='" + lastIp + '\'' + + ", lastVistit=" + lastVistit + + '}'; + } +} diff --git a/spring-bbs-demo/src/main/java/com/springbbs/service/UserService.java b/spring-bbs-demo/src/main/java/com/springbbs/service/UserService.java new file mode 100644 index 0000000..ec307fd --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/service/UserService.java @@ -0,0 +1,43 @@ +package com.springbbs.service; + +import com.springbbs.dao.LoginLogDao; +import com.springbbs.dao.UserDao; +import com.springbbs.domain.LoginLog; +import com.springbbs.domain.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + * @Description 业务层,负责调用DAO层 + * Created by junqiangshen on 15-8-27. + */ +@Service // 将UserService标注为一个服务层的Bean +public class UserService { + + @Autowired + private UserDao userDao; + + @Autowired + private LoginLogDao loginLogDao; + + public boolean hasMatchUser(String userName, String password) { + int matchCount = userDao.getMatchCount(userName, password); + + return matchCount > 0; + } + + public User findUserByUserName(String userName) { + return userDao.findUserByUserName(userName); + } + + public void loginSucess(User user) { + user.setCredits(5+ user.getCredits()); + LoginLog loginLog = new LoginLog(); + loginLog.setUserId(user.getUserId()); + loginLog.setIp(user.getLastIp()); + loginLog.setLoginDate(user.getLastVistit()); + + userDao.updateLoginInfo(user); + loginLogDao.insertLoginLog(loginLog); + } +} diff --git a/spring-bbs-demo/src/main/resources/applicationContext.xml b/spring-bbs-demo/src/main/resources/applicationContext.xml new file mode 100644 index 0000000..a335128 --- /dev/null +++ b/spring-bbs-demo/src/main/resources/applicationContext.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + p:driverClassName="${jdbc.driver}" + p:url="${jdbc.url}" + p:username="${jdbc.username}" + p:password="${jdbc.password}"/> + + + + \ No newline at end of file diff --git a/spring-bbs-demo/src/main/resources/jdbc.properties b/spring-bbs-demo/src/main/resources/jdbc.properties new file mode 100644 index 0000000..549d81e --- /dev/null +++ b/spring-bbs-demo/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +jdbc.driver=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/sampledb +jdbc.username=root +jdbc.password=root0724 \ No newline at end of file diff --git a/spring-bbs-demo/src/main/resources/sql/user_table_init.sql b/spring-bbs-demo/src/main/resources/sql/user_table_init.sql new file mode 100644 index 0000000..cbec780 --- /dev/null +++ b/spring-bbs-demo/src/main/resources/sql/user_table_init.sql @@ -0,0 +1,26 @@ +DROP DATABASE IF EXISTS sampledb; +CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8mb4; +USE sampledb; + +-- 创建用户表 +CREATE TABLE t_user ( + user_id INT AUTO_INCREMENT NOT NULL COMMENT '用户id', + user_name VARCHAR(30) NOT NULL DEFAULT '' COMMENT '用户名', + credits INT NOT NULL DEFAULT 0 COMMENT '论坛积分', + password VARCHAR(32) NOT NULL DEFAULT ''COMMENT '用户密码', + last_visit TIMESTAMP NOT NULL DEFAULT 0 COMMENT '最后访问时间', + last_ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '最后访问ip', + PRIMARY KEY (user_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户表'; + +-- 创建用户登陆日志表 +CREATE TABLE t_login_log ( + login_log_id INT AUTO_INCREMENT NOT NULL COMMENT '日志id', + user_id INT NOT NULL DEFAULT 0 COMMENT '用户id', + ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '访问ip', + login_datetime TIMESTAMP NOT NULL DEFAULT 0 COMMENT '访问时间', + PRIMARY KEY (login_log_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户登陆日志表'; + +INSERT INTO t_user (user_name,password) VALUES ('admin','123456'); +SELECT * FROM t_user; \ No newline at end of file diff --git a/spring-bbs-demo/src/main/test/java/com/springbbs/service/UserServiceTest.java b/spring-bbs-demo/src/main/test/java/com/springbbs/service/UserServiceTest.java new file mode 100644 index 0000000..8a9a3cc --- /dev/null +++ b/spring-bbs-demo/src/main/test/java/com/springbbs/service/UserServiceTest.java @@ -0,0 +1,42 @@ +package com.springbbs.service; + +import com.springbbs.domain.User; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Date; + +@RunWith(SpringJUnit4ClassRunner.class) // 基于JUnit4的Spring测试框架 +@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //启动Spring容器 +public class UserServiceTest { + + @Autowired + private UserService userService; + + @Test + public void testHasMatchUser() throws Exception { + boolean b1 = userService.hasMatchUser("admin", "123456"); + boolean b2 = userService.hasMatchUser("admin", "1111"); + + Assert.assertEquals(true, b1); + Assert.assertEquals(false, b2); + } + + @Test + public void testFindUserByUserName() throws Exception { + User user = userService.findUserByUserName("admin"); + Assert.assertEquals("admin", user.getUserName()); + } + + @Test + public void testLoginSucess() throws Exception { + User user = userService.findUserByUserName("admin"); + user.setLastIp("127.0.0.1"); + user.setLastVistit(new Date()); + userService.loginSucess(user); + } +} \ No newline at end of file diff --git a/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml b/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/spring-bbs-demo/src/main/webapp/index.jsp b/spring-bbs-demo/src/main/webapp/index.jsp new file mode 100644 index 0000000..c38169b --- /dev/null +++ b/spring-bbs-demo/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/spring-bbs-demo/target/MANIFEST.MF b/spring-bbs-demo/target/MANIFEST.MF new file mode 100644 index 0000000..5112119 --- /dev/null +++ b/spring-bbs-demo/target/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Build-Jdk: 1.7.0_40 +Built-By: junqiangshen +Created-By: IntelliJ IDEA + diff --git a/spring-bbs-demo/target/classes/applicationContext.xml b/spring-bbs-demo/target/classes/applicationContext.xml new file mode 100644 index 0000000..19fc6c1 --- /dev/null +++ b/spring-bbs-demo/target/classes/applicationContext.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class b/spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class new file mode 100644 index 0000000000000000000000000000000000000000..39c16ca90680d12f9cda29731adbe42a85fd599a GIT binary patch literal 1228 zcma)5>rN9v6#k|yE!0(@fQpI~ylpSADqheUl?Wy^wIsCE5Py(qJJiALPTieC_~kR1 z7&IpO06vuQOev*eW6UP!?40X&zBBXl*S8-4mXM1hjJaXV$8ar<>$nleP24hnwh^fj z#F368gKP{74AySFS}TiY%jH@yWDCk~I6=#oUSr4SO|h^1_l}8iYRVVRtG?(Q!}Lbm z)3PZlGLThQtazT%TubEz49f#cRR#9~XNT9c@(&!|OK?`&TJ1|;)TwOriS(p?$`H0w z6^6(QRVQqt1?h=myIB=}nNv50L_yWKTj9Pm=dLlL-$`meTd1jKcamx~aOzw+1=Wxq ziB+zs%Q@)+}hI#vRj%sr0_0n<%%&^-nGN8q&a2h1|;Fh#+knZw5?zr4*IAkDp z2|$GV4MIBcw$Hg<-4!)W&Ui_+{hD|w&7fn)APM=Rsl<>ONN=EpFO)uL31>sJR3Ht_ zISZp0V|bn~mew}P$$YWAp48i&L5#H9#KX3%XJspUWUSL=FndiUD=US~wUYfTOJ9nB zoQ!dVG=@bB4sOSA$HHAKS-6M$79QZCg)^{XShny8j~QnExsJd6e{#t%(hz!+aOUfs zYmsLdKf#jsv}g$8Ikv~$wpia`IB(}ueY|!fh}a=2ly5Oi+Wr3CQY>o^HPZt54-IM{hJtubClaNF;rhPEpe5NQX$%K1q(# z$sx^152e3=ed@RqBuy|VZGxmJKmnM<6ltOc7f71qkBHC*WD=p*(eDUvg)@hUe8o^{ zD?)$K($)~ESgDx)jN$CUAx7Suv`BPPCkW{jl2kNJWlg*EQORyQDvpboriNkIxP;4u WKS@(v!31%$%sSQp literal 0 HcmV?d00001 diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class b/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class new file mode 100644 index 0000000000000000000000000000000000000000..cf951d663dd4ce1b7d6e6d2a7ef339de9d566789 GIT binary patch literal 1242 zcmZ`&Yg5xe6g}HkQralB@(}TD#Wp+w;u}Pv;MlR^NR_Y7Hd#tan#Cjq{aMZkj(+e5 z_@f-}rlswanwi|q?!9~Nx%b@s`TOfPfF*1WBaOT=1!asBbj&0%o5mdGQ!ufh%wiJv z)w-nNfrf_~9x)^jxtnVS!e^K&Te4vW2fpLgs@1@>WrI7O`H`H<4Ekx{7H!LVg<^)nJtxR541nS?!r92}EfV|GfiEo) z1l#h6VItq+5`1>eZ4orxup+``#b($)-7<_03L>Ph4n?i#c#c&K zFLb=bnvQi8$!+<k@WY?x?QzQ zOZvj3&J?-pR=KtRntQgZhHETBQEjTcFL&x@nEY>Unz3ESvsD|`zbe}rJhR>L-pMQU zOV%?h4Dr0uBikX+(TOIpCc=_O6^#u-EuE#;1tx_95h`9Kn`SRxQc2S7qZZYHpcIv& zsA5Y2+@#-Cry)F#cqXG{1ZX8Cqm{I!|2szSD8?vioW53RpHL^p%rTfTdyJSdcZ|64 z1Bq`@^aR-V#9u3#Dk+>5e}w-DRe00TFuN=95jHyCbH&O7uSLym5$vuV0HMPDiM IrfH?;A71`1;Q#;t literal 0 HcmV?d00001 diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class b/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class new file mode 100644 index 0000000000000000000000000000000000000000..07bb6f7892b1c8ba17a0e2c43e97366d3f4dbc4b GIT binary patch literal 2137 zcma)8T~pge6g`V=Y>WcINt>ogh-lMd8xsZSM_ThyFs2v`q_%;&ePCpHahAxEBgrsK z|56{?nGSTCX{XQqQJvnEh3x{vzzn;0rL*^*d-mRy|M~ZizW~gk8b=tjG0Y9&J$x3$ z=TUqS$Cr2+M+9HR@O2c~IEFAA#e5vYSdcFlV_1?8zmdbT99ASeC#_Wp$Vaip00yOC zS=`B)y6bY6mPhrPQQ^&&se7CuQ?#9i=C&MRH4YuU$xm$OjV3XgVLM#g=tE~29+ujc zCz^auxT0qAWy`WX-4nJ&o?l+;s>XH8)ed#Tvz=2-?{3hR+n#+Q99}24gY&`?-XcRN znc8Cr=j=Lh8!ZZpZ?~H@?o@P&#*ir5hHmcZj*#o%Vc2^uDEy$bM* za%WYyDZ-rw_qKG;c%8E;1BS_D@kl?`HB+}5TG^B7XRkg<6&PY|aH<1p@(AQ-4KhS8qRi~pY+6`e9 ztV5e&Jb8mxsc*b&w;hA838|?f8InlPWo`^p*Pf#5afjR9X^U$m-m+aGPcy6FF`h8Y zm-EGZuA&-J+GIMVu9bGSRPR8tSNGTRrMxQ51B*mlSWFG+MUwYT8 zre`xU@1lY&Y%{FvzFJ+berJt_?8+qW;n$lEq|PSW zY6M|h{6I6qVWRPmx6Q;IGeMW6L6o3jXhWi(Q{)>bma7q(#(qbl zTpc9qp0v(zf4?X9i0?w9aR#&WBS0#ncHDLe&H2HpoA4qa6r$6Uy1wY1?+mRU(0`oB J4bx12{{aDeB2EAR literal 0 HcmV?d00001 diff --git a/spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class b/spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class new file mode 100644 index 0000000000000000000000000000000000000000..a33bf994d01adec393073a796bcab82af3724a3c GIT binary patch literal 1749 zcmaKr-BQy~5Xb*%n|_%}Enmuq6j57BAt zYt=QQx-1}KpIKKdmF{x_nwF-^?4j-8ie_lmssMj_dS5^wZ&oO9_Y#z#u8sAtdHROtUsAz^voHi`o6053KXUVv98PUfY zXT5=Rff*tcyw|QPLLr^GNd5wqIgp3`c6Vu;L z)eYf9Vas&7OM>j0&3akg)VM{lPK9$UgM=g|C0xUZgi$tQ7?*Gz69R_aGOaZ=y`t8s zv`&f-`iasx30ru zQ+1^u<4tqlLJ;ki}DlY@9sM`*zMq!?F#g(K?XClAQ zXuf#ZX85Ulqmqcv_>^Oq%CJT`UX2byS&M2We8YV{(ho~m7^1yd#C+s1G&&Zcjco)> z^WCs%?8=_o?=`I*TS}Xi>Rgh}>i8JTI!@uVjt{Y<f4=` z)l;C$hkoe?wJz&;fQKR;@nUsJv{JV+zyAO~1iInil$S}gx)cphIXHy#BRBcgrSS{} zpZ!tCO{^(cnAmoX+L-cgc)f|Y#>R<!ATIV3j9U3YRF~cK#bvf5e>U6P*ow z5;<>@d`d!<7N28k96A?h7m#L#68?B-6@?cx$ycZ^V=Vz>`p+21sYH+~`1BCSh3A-# zgPbAA*$E(0Z~?iR2$G6}oKFP#EEyyf2U#IVX#$9JTtKcRwk92K&Ba8J&y!n|j)Pn# z$l3%D894!2Ppn8LUXiPbAlH*Cl8J+?6Xf~?5Se)a*}x6T_^_&Rka8kO8Jm&wcvv|j zY8>PiLAEA<$fFYw{%1ws`i%oEPpmv{=6v?lYsyLa0y&I5zt$-3OS9r@He-s_oIE{vvoZxBdq#X?0Hk literal 0 HcmV?d00001 diff --git a/spring-bbs-demo/target/classes/com/springbbs/service/UserService.class b/spring-bbs-demo/target/classes/com/springbbs/service/UserService.class new file mode 100644 index 0000000000000000000000000000000000000000..90fb95cd6222345152a72f75c3c47d9b48ecde66 GIT binary patch literal 1878 zcma)6ZF3V<6n<`!c9UhHkQTIH3lx+jt*inn5~!3`G@6!|KoESrNw(>QWH+O{3%N%zZuQIp;a&+56|;zx@H=GQKr2iz_+g@t%qI z@qr$$n)ndc^iVc%-GqTFdUHdsR`u#5y}GGaYkGA{JFRPs4HF;Zwt-I!d@5k{1IOQx zp1`SU)9czn&sT1{(Fp98^z4W6Wm#bUe%}pM*Qu*OH9F3k>w2LKmFETmSF4`iPCU1L z*>zrc{xiGbNH?&zWi#~rU0aSE?6rRAy->c>BJ81#*H&)TYey-Zo{$3Xh6c2szOGyq zt_q|J#kxRd-D^1lGgaj}Tm5ds@gGQLCy=jtP1&hSU+H~vm*s9g&d!&rW28^ z#cM{gc4V+A!{&~zfxv}A^{IR=?T&QYb}iIJDjz&4J`u?3if*wAI*S87yzj{%V1-%& zW>*JT_gGp1Q4u&?3uW`!rtA%AY%8~=6S%dj#~9U~kVedvjD%sl|o9s0$=en5dv2bbQAP zcYBVVn3pY_#3>7NIBuX~;WK=0p@@=!s)bE#S-69{0t;hG-k*t zTX=*oEIh^&3twWz!t0nf@RfzH@r}UQ{di1&Trgy{*Xlcsr%sa{o!{^F$`8d(x1BJO zSJM6esV{0|9CfcNtTR#9`Ddw+edaGhb2E75C{w8D2Ipv|n^2(!bG#p|d0v%4Sm_DO z6^fJ8J4JJSeSDG#39Wf)2)+}cVh!>`n0}}__J#}{{cjN@Qit9P%qCcSCOXNPey_#r z$gbjU^Mx93D<7z##-iurh;m6PM&^aU(!VHu=v6M(ah6ZwET7pFKgtRMC%BvD{91HM zA&i}6xTgv^zCjC{v*0{cdI|AMhPIE^#hQBGLmsGVJ;k>Na7`W30= zl>ySL3qQcf{D@5YC!VLFu}r20D$-+Faq^2%uoF@6+$dNs2_~?Nl?3X021?VryqsMg zVCuU8rhkX=_$9KND?G>zVCwnM0G6H)4{#(2I>Y%Wc^})W+!FHzD`YZIo@`E%(-PT~ n$mJ6Azs8lW!kIYaD6ul$#ziKSr;UwwB6jaalrD{0dKvf!Nnpg$ literal 0 HcmV?d00001 diff --git a/spring-bbs-demo/target/classes/jdbc.properties b/spring-bbs-demo/target/classes/jdbc.properties new file mode 100644 index 0000000..549d81e --- /dev/null +++ b/spring-bbs-demo/target/classes/jdbc.properties @@ -0,0 +1,4 @@ +jdbc.driver=com.mysql.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/sampledb +jdbc.username=root +jdbc.password=root0724 \ No newline at end of file diff --git a/spring-bbs-demo/target/classes/sql/user_table_init.sql b/spring-bbs-demo/target/classes/sql/user_table_init.sql new file mode 100644 index 0000000..cbec780 --- /dev/null +++ b/spring-bbs-demo/target/classes/sql/user_table_init.sql @@ -0,0 +1,26 @@ +DROP DATABASE IF EXISTS sampledb; +CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8mb4; +USE sampledb; + +-- 创建用户表 +CREATE TABLE t_user ( + user_id INT AUTO_INCREMENT NOT NULL COMMENT '用户id', + user_name VARCHAR(30) NOT NULL DEFAULT '' COMMENT '用户名', + credits INT NOT NULL DEFAULT 0 COMMENT '论坛积分', + password VARCHAR(32) NOT NULL DEFAULT ''COMMENT '用户密码', + last_visit TIMESTAMP NOT NULL DEFAULT 0 COMMENT '最后访问时间', + last_ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '最后访问ip', + PRIMARY KEY (user_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户表'; + +-- 创建用户登陆日志表 +CREATE TABLE t_login_log ( + login_log_id INT AUTO_INCREMENT NOT NULL COMMENT '日志id', + user_id INT NOT NULL DEFAULT 0 COMMENT '用户id', + ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '访问ip', + login_datetime TIMESTAMP NOT NULL DEFAULT 0 COMMENT '访问时间', + PRIMARY KEY (login_log_id) +)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户登陆日志表'; + +INSERT INTO t_user (user_name,password) VALUES ('admin','123456'); +SELECT * FROM t_user; \ No newline at end of file diff --git a/spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class b/spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class new file mode 100644 index 0000000000000000000000000000000000000000..451004e25d3c096d15cf25651c1cd14ca870dc66 GIT binary patch literal 1955 zcmah}e^(Pn6um=$Kv;elFch$g1xo@gYXHRx+6IKGjbII6)xS2&k__GKruzf&kAA40 z(}SM&oPL0QC{OR~?nz=xfgJYDyqS0Jee>ST-yi?@3&1koWROL%A6Kx{hvh!3WN-&} z`>>k9J$#eFeXJ?ZdIk^hun*rV`&}BP3QAeAp12y}0{hmncBiu4++0yjFuHfy~ya zX*Hv?(%5D|l-Y4eje3HSQyAOQIaKi*@6>mPDN}nG0Z#o(&7s>cq!XPc6%4EBvRji* zB{VIc>N6EL^h|48sx-$GTqQ1m;#)j6P7Hy`4jEqPIF_fg94}>1CuGlwVTX(@om&OP zr<<-5Sf_y=C9}q(9AJcfDp@KcK7ME5RZ}V5wun#uSkUB6u45Y0;G6GA(OZ=qa6gU0fmNhRPcvTp@t!w;S`1+Jb)PpyN=9zIWjR2571 zwK`!3G_|@VKxQ5R;6S&UcR22LxZHhj9{&#d`Q`zn_^sFc2UXCAhe?WSQ{~yqo zqPZ3i+cY`!Fg!;JrDy^(Bwr+{qMeSR6IzXHH|P`GH_i;FDfA2OQrXP3@|&fH7p5if zId^r@Hx`RJLw`A&S~x?7g4!yEBBmS25OtK;iai&{CF~ileTg|@{~u?*#aSns;+$=B zE@TJJpe_7~!B_7wbcSKIL>VJ(4e?3big-rIG=)6#Ds*U>ZE2ajP)ir1R2k=^QyJ%I Sfx5pk@imWDKz{?_ Date: Fri, 28 Aug 2015 00:38:26 +0800 Subject: [PATCH 04/10] =?UTF-8?q?spring=20bbs=20dem=20=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.MD | 3 + spring-bbs-demo/README.MD | 4 +- .../src/main/resources/applicationContext.xml | 2 +- spring-bbs-demo/target/MANIFEST.MF | 5 - .../target/classes/applicationContext.xml | 50 ----- .../com/springbbs/dao/LoginLogDao.class | Bin 1228 -> 0 bytes .../classes/com/springbbs/dao/UserDao$1.class | Bin 1242 -> 0 bytes .../classes/com/springbbs/dao/UserDao.class | Bin 2137 -> 0 bytes .../com/springbbs/domain/LoginLog.class | Bin 1749 -> 0 bytes .../classes/com/springbbs/domain/User.class | Bin 2199 -> 0 bytes .../com/springbbs/service/UserService.class | Bin 1878 -> 0 bytes .../target/classes/jdbc.properties | 4 - .../target/classes/sql/user_table_init.sql | 26 --- .../springbbs/service/UserServiceTest.class | Bin 1955 -> 0 bytes .../com/learn/note/practice/ioc/README.MD | 173 ------------------ .../practice/ioc/common/beans/UserBean.java | 45 ----- src/main/resources/config.xml | 9 - .../ioc/common/beans/UserBeanTest.java | 23 --- 19 files changed, 7 insertions(+), 338 deletions(-) delete mode 100644 spring-bbs-demo/target/MANIFEST.MF delete mode 100644 spring-bbs-demo/target/classes/applicationContext.xml delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/domain/User.class delete mode 100644 spring-bbs-demo/target/classes/com/springbbs/service/UserService.class delete mode 100644 spring-bbs-demo/target/classes/jdbc.properties delete mode 100644 spring-bbs-demo/target/classes/sql/user_table_init.sql delete mode 100644 spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class delete mode 100644 src/main/java/com/learn/note/practice/ioc/README.MD delete mode 100644 src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java delete mode 100644 src/main/resources/config.xml delete mode 100644 src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java diff --git a/.gitignore b/.gitignore index 959c62b..8c293a4 100755 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # 忽略classes目录 target/ +*/target/ # 忽略工程文件 *.iml diff --git a/README.MD b/README.MD index b1e1226..edef856 100644 --- a/README.MD +++ b/README.MD @@ -1,5 +1,8 @@ ### Java学习代码仓库 +#### 模块目录 +* 用Spring实现的一个论坛的基本功能(spring-bbs-demo) + #### 项目初始化 新建一个maven工程,使用了公司的仓库模板,过程大概如下: * 1.new->project->maven->create from archetype->maven-archetype-quickstart diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index e8521c8..6cc6731 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -109,9 +109,9 @@ jdbcTemplate#query() | `query(String sql,Object[] args,RowCallbackHandler rch)`. + p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" @@ -181,9 +181,9 @@ jdbc.password=root0724 + p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" diff --git a/spring-bbs-demo/src/main/resources/applicationContext.xml b/spring-bbs-demo/src/main/resources/applicationContext.xml index a335128..afe2af2 100644 --- a/spring-bbs-demo/src/main/resources/applicationContext.xml +++ b/spring-bbs-demo/src/main/resources/applicationContext.xml @@ -37,9 +37,9 @@ + p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" diff --git a/spring-bbs-demo/target/MANIFEST.MF b/spring-bbs-demo/target/MANIFEST.MF deleted file mode 100644 index 5112119..0000000 --- a/spring-bbs-demo/target/MANIFEST.MF +++ /dev/null @@ -1,5 +0,0 @@ -Manifest-Version: 1.0 -Build-Jdk: 1.7.0_40 -Built-By: junqiangshen -Created-By: IntelliJ IDEA - diff --git a/spring-bbs-demo/target/classes/applicationContext.xml b/spring-bbs-demo/target/classes/applicationContext.xml deleted file mode 100644 index 19fc6c1..0000000 --- a/spring-bbs-demo/target/classes/applicationContext.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class b/spring-bbs-demo/target/classes/com/springbbs/dao/LoginLogDao.class deleted file mode 100644 index 39c16ca90680d12f9cda29731adbe42a85fd599a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1228 zcma)5>rN9v6#k|yE!0(@fQpI~ylpSADqheUl?Wy^wIsCE5Py(qJJiALPTieC_~kR1 z7&IpO06vuQOev*eW6UP!?40X&zBBXl*S8-4mXM1hjJaXV$8ar<>$nleP24hnwh^fj z#F368gKP{74AySFS}TiY%jH@yWDCk~I6=#oUSr4SO|h^1_l}8iYRVVRtG?(Q!}Lbm z)3PZlGLThQtazT%TubEz49f#cRR#9~XNT9c@(&!|OK?`&TJ1|;)TwOriS(p?$`H0w z6^6(QRVQqt1?h=myIB=}nNv50L_yWKTj9Pm=dLlL-$`meTd1jKcamx~aOzw+1=Wxq ziB+zs%Q@)+}hI#vRj%sr0_0n<%%&^-nGN8q&a2h1|;Fh#+knZw5?zr4*IAkDp z2|$GV4MIBcw$Hg<-4!)W&Ui_+{hD|w&7fn)APM=Rsl<>ONN=EpFO)uL31>sJR3Ht_ zISZp0V|bn~mew}P$$YWAp48i&L5#H9#KX3%XJspUWUSL=FndiUD=US~wUYfTOJ9nB zoQ!dVG=@bB4sOSA$HHAKS-6M$79QZCg)^{XShny8j~QnExsJd6e{#t%(hz!+aOUfs zYmsLdKf#jsv}g$8Ikv~$wpia`IB(}ueY|!fh}a=2ly5Oi+Wr3CQY>o^HPZt54-IM{hJtubClaNF;rhPEpe5NQX$%K1q(# z$sx^152e3=ed@RqBuy|VZGxmJKmnM<6ltOc7f71qkBHC*WD=p*(eDUvg)@hUe8o^{ zD?)$K($)~ESgDx)jN$CUAx7Suv`BPPCkW{jl2kNJWlg*EQORyQDvpboriNkIxP;4u WKS@(v!31%$%sSQp diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class b/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao$1.class deleted file mode 100644 index cf951d663dd4ce1b7d6e6d2a7ef339de9d566789..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1242 zcmZ`&Yg5xe6g}HkQralB@(}TD#Wp+w;u}Pv;MlR^NR_Y7Hd#tan#Cjq{aMZkj(+e5 z_@f-}rlswanwi|q?!9~Nx%b@s`TOfPfF*1WBaOT=1!asBbj&0%o5mdGQ!ufh%wiJv z)w-nNfrf_~9x)^jxtnVS!e^K&Te4vW2fpLgs@1@>WrI7O`H`H<4Ekx{7H!LVg<^)nJtxR541nS?!r92}EfV|GfiEo) z1l#h6VItq+5`1>eZ4orxup+``#b($)-7<_03L>Ph4n?i#c#c&K zFLb=bnvQi8$!+<k@WY?x?QzQ zOZvj3&J?-pR=KtRntQgZhHETBQEjTcFL&x@nEY>Unz3ESvsD|`zbe}rJhR>L-pMQU zOV%?h4Dr0uBikX+(TOIpCc=_O6^#u-EuE#;1tx_95h`9Kn`SRxQc2S7qZZYHpcIv& zsA5Y2+@#-Cry)F#cqXG{1ZX8Cqm{I!|2szSD8?vioW53RpHL^p%rTfTdyJSdcZ|64 z1Bq`@^aR-V#9u3#Dk+>5e}w-DRe00TFuN=95jHyCbH&O7uSLym5$vuV0HMPDiM IrfH?;A71`1;Q#;t diff --git a/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class b/spring-bbs-demo/target/classes/com/springbbs/dao/UserDao.class deleted file mode 100644 index 07bb6f7892b1c8ba17a0e2c43e97366d3f4dbc4b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2137 zcma)8T~pge6g`V=Y>WcINt>ogh-lMd8xsZSM_ThyFs2v`q_%;&ePCpHahAxEBgrsK z|56{?nGSTCX{XQqQJvnEh3x{vzzn;0rL*^*d-mRy|M~ZizW~gk8b=tjG0Y9&J$x3$ z=TUqS$Cr2+M+9HR@O2c~IEFAA#e5vYSdcFlV_1?8zmdbT99ASeC#_Wp$Vaip00yOC zS=`B)y6bY6mPhrPQQ^&&se7CuQ?#9i=C&MRH4YuU$xm$OjV3XgVLM#g=tE~29+ujc zCz^auxT0qAWy`WX-4nJ&o?l+;s>XH8)ed#Tvz=2-?{3hR+n#+Q99}24gY&`?-XcRN znc8Cr=j=Lh8!ZZpZ?~H@?o@P&#*ir5hHmcZj*#o%Vc2^uDEy$bM* za%WYyDZ-rw_qKG;c%8E;1BS_D@kl?`HB+}5TG^B7XRkg<6&PY|aH<1p@(AQ-4KhS8qRi~pY+6`e9 ztV5e&Jb8mxsc*b&w;hA838|?f8InlPWo`^p*Pf#5afjR9X^U$m-m+aGPcy6FF`h8Y zm-EGZuA&-J+GIMVu9bGSRPR8tSNGTRrMxQ51B*mlSWFG+MUwYT8 zre`xU@1lY&Y%{FvzFJ+berJt_?8+qW;n$lEq|PSW zY6M|h{6I6qVWRPmx6Q;IGeMW6L6o3jXhWi(Q{)>bma7q(#(qbl zTpc9qp0v(zf4?X9i0?w9aR#&WBS0#ncHDLe&H2HpoA4qa6r$6Uy1wY1?+mRU(0`oB J4bx12{{aDeB2EAR diff --git a/spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class b/spring-bbs-demo/target/classes/com/springbbs/domain/LoginLog.class deleted file mode 100644 index a33bf994d01adec393073a796bcab82af3724a3c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1749 zcmaKr-BQy~5Xb*%n|_%}Enmuq6j57BAt zYt=QQx-1}KpIKKdmF{x_nwF-^?4j-8ie_lmssMj_dS5^wZ&oO9_Y#z#u8sAtdHROtUsAz^voHi`o6053KXUVv98PUfY zXT5=Rff*tcyw|QPLLr^GNd5wqIgp3`c6Vu;L z)eYf9Vas&7OM>j0&3akg)VM{lPK9$UgM=g|C0xUZgi$tQ7?*Gz69R_aGOaZ=y`t8s zv`&f-`iasx30ru zQ+1^u<4tqlLJ;ki}DlY@9sM`*zMq!?F#g(K?XClAQ zXuf#ZX85Ulqmqcv_>^Oq%CJT`UX2byS&M2We8YV{(ho~m7^1yd#C+s1G&&Zcjco)> z^WCs%?8=_o?=`I*TS}Xi>Rgh}>i8JTI!@uVjt{Y<f4=` z)l;C$hkoe?wJz&;fQKR;@nUsJv{JV+zyAO~1iInil$S}gx)cphIXHy#BRBcgrSS{} zpZ!tCO{^(cnAmoX+L-cgc)f|Y#>R<!ATIV3j9U3YRF~cK#bvf5e>U6P*ow z5;<>@d`d!<7N28k96A?h7m#L#68?B-6@?cx$ycZ^V=Vz>`p+21sYH+~`1BCSh3A-# zgPbAA*$E(0Z~?iR2$G6}oKFP#EEyyf2U#IVX#$9JTtKcRwk92K&Ba8J&y!n|j)Pn# z$l3%D894!2Ppn8LUXiPbAlH*Cl8J+?6Xf~?5Se)a*}x6T_^_&Rka8kO8Jm&wcvv|j zY8>PiLAEA<$fFYw{%1ws`i%oEPpmv{=6v?lYsyLa0y&I5zt$-3OS9r@He-s_oIE{vvoZxBdq#X?0Hk diff --git a/spring-bbs-demo/target/classes/com/springbbs/service/UserService.class b/spring-bbs-demo/target/classes/com/springbbs/service/UserService.class deleted file mode 100644 index 90fb95cd6222345152a72f75c3c47d9b48ecde66..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1878 zcma)6ZF3V<6n<`!c9UhHkQTIH3lx+jt*inn5~!3`G@6!|KoESrNw(>QWH+O{3%N%zZuQIp;a&+56|;zx@H=GQKr2iz_+g@t%qI z@qr$$n)ndc^iVc%-GqTFdUHdsR`u#5y}GGaYkGA{JFRPs4HF;Zwt-I!d@5k{1IOQx zp1`SU)9czn&sT1{(Fp98^z4W6Wm#bUe%}pM*Qu*OH9F3k>w2LKmFETmSF4`iPCU1L z*>zrc{xiGbNH?&zWi#~rU0aSE?6rRAy->c>BJ81#*H&)TYey-Zo{$3Xh6c2szOGyq zt_q|J#kxRd-D^1lGgaj}Tm5ds@gGQLCy=jtP1&hSU+H~vm*s9g&d!&rW28^ z#cM{gc4V+A!{&~zfxv}A^{IR=?T&QYb}iIJDjz&4J`u?3if*wAI*S87yzj{%V1-%& zW>*JT_gGp1Q4u&?3uW`!rtA%AY%8~=6S%dj#~9U~kVedvjD%sl|o9s0$=en5dv2bbQAP zcYBVVn3pY_#3>7NIBuX~;WK=0p@@=!s)bE#S-69{0t;hG-k*t zTX=*oEIh^&3twWz!t0nf@RfzH@r}UQ{di1&Trgy{*Xlcsr%sa{o!{^F$`8d(x1BJO zSJM6esV{0|9CfcNtTR#9`Ddw+edaGhb2E75C{w8D2Ipv|n^2(!bG#p|d0v%4Sm_DO z6^fJ8J4JJSeSDG#39Wf)2)+}cVh!>`n0}}__J#}{{cjN@Qit9P%qCcSCOXNPey_#r z$gbjU^Mx93D<7z##-iurh;m6PM&^aU(!VHu=v6M(ah6ZwET7pFKgtRMC%BvD{91HM zA&i}6xTgv^zCjC{v*0{cdI|AMhPIE^#hQBGLmsGVJ;k>Na7`W30= zl>ySL3qQcf{D@5YC!VLFu}r20D$-+Faq^2%uoF@6+$dNs2_~?Nl?3X021?VryqsMg zVCuU8rhkX=_$9KND?G>zVCwnM0G6H)4{#(2I>Y%Wc^})W+!FHzD`YZIo@`E%(-PT~ n$mJ6Azs8lW!kIYaD6ul$#ziKSr;UwwB6jaalrD{0dKvf!Nnpg$ diff --git a/spring-bbs-demo/target/classes/jdbc.properties b/spring-bbs-demo/target/classes/jdbc.properties deleted file mode 100644 index 549d81e..0000000 --- a/spring-bbs-demo/target/classes/jdbc.properties +++ /dev/null @@ -1,4 +0,0 @@ -jdbc.driver=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/sampledb -jdbc.username=root -jdbc.password=root0724 \ No newline at end of file diff --git a/spring-bbs-demo/target/classes/sql/user_table_init.sql b/spring-bbs-demo/target/classes/sql/user_table_init.sql deleted file mode 100644 index cbec780..0000000 --- a/spring-bbs-demo/target/classes/sql/user_table_init.sql +++ /dev/null @@ -1,26 +0,0 @@ -DROP DATABASE IF EXISTS sampledb; -CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8mb4; -USE sampledb; - --- 创建用户表 -CREATE TABLE t_user ( - user_id INT AUTO_INCREMENT NOT NULL COMMENT '用户id', - user_name VARCHAR(30) NOT NULL DEFAULT '' COMMENT '用户名', - credits INT NOT NULL DEFAULT 0 COMMENT '论坛积分', - password VARCHAR(32) NOT NULL DEFAULT ''COMMENT '用户密码', - last_visit TIMESTAMP NOT NULL DEFAULT 0 COMMENT '最后访问时间', - last_ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '最后访问ip', - PRIMARY KEY (user_id) -)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户表'; - --- 创建用户登陆日志表 -CREATE TABLE t_login_log ( - login_log_id INT AUTO_INCREMENT NOT NULL COMMENT '日志id', - user_id INT NOT NULL DEFAULT 0 COMMENT '用户id', - ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '访问ip', - login_datetime TIMESTAMP NOT NULL DEFAULT 0 COMMENT '访问时间', - PRIMARY KEY (login_log_id) -)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户登陆日志表'; - -INSERT INTO t_user (user_name,password) VALUES ('admin','123456'); -SELECT * FROM t_user; \ No newline at end of file diff --git a/spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class b/spring-bbs-demo/target/test-classes/com/springbbs/service/UserServiceTest.class deleted file mode 100644 index 451004e25d3c096d15cf25651c1cd14ca870dc66..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1955 zcmah}e^(Pn6um=$Kv;elFch$g1xo@gYXHRx+6IKGjbII6)xS2&k__GKruzf&kAA40 z(}SM&oPL0QC{OR~?nz=xfgJYDyqS0Jee>ST-yi?@3&1koWROL%A6Kx{hvh!3WN-&} z`>>k9J$#eFeXJ?ZdIk^hun*rV`&}BP3QAeAp12y}0{hmncBiu4++0yjFuHfy~ya zX*Hv?(%5D|l-Y4eje3HSQyAOQIaKi*@6>mPDN}nG0Z#o(&7s>cq!XPc6%4EBvRji* zB{VIc>N6EL^h|48sx-$GTqQ1m;#)j6P7Hy`4jEqPIF_fg94}>1CuGlwVTX(@om&OP zr<<-5Sf_y=C9}q(9AJcfDp@KcK7ME5RZ}V5wun#uSkUB6u45Y0;G6GA(OZ=qa6gU0fmNhRPcvTp@t!w;S`1+Jb)PpyN=9zIWjR2571 zwK`!3G_|@VKxQ5R;6S&UcR22LxZHhj9{&#d`Q`zn_^sFc2UXCAhe?WSQ{~yqo zqPZ3i+cY`!Fg!;JrDy^(Bwr+{qMeSR6IzXHH|P`GH_i;FDfA2OQrXP3@|&fH7p5if zId^r@Hx`RJLw`A&S~x?7g4!yEBBmS25OtK;iai&{CF~ileTg|@{~u?*#aSns;+$=B zE@TJJpe_7~!B_7wbcSKIL>VJ(4e?3big-rIG=)6#Ds*U>ZE2ajP)ir1R2k=^QyJ%I Sfx5pk@imWDKz{?_ - - - - - - - -``` -然后创建一个单元测试`UserBeanTest.java` -```java -package com.learn.note.practice.ioc.common.beans; - -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import static org.junit.Assert.*; - -public class UserBeanTest { - - @Test - public void testUserBeanSpring() { - ApplicationContext applicationContext = - new ClassPathXmlApplicationContext(new String[] {"config.xml"}); - // 取得一个实例 - UserBean user = (UserBean) applicationContext.getBean("userBean"); - System.out.println(user); - } -} - -``` -注意最终的目录结构是这样的 -``` -. -├── JavaPracticeCode.iml -├── pom.xml -├── README.MD -├── src -│   ├── main -│   │   ├── java -│   │   │   └── com -│   │   │   └── learn -│   │   │   └── note -│   │   │   └── practice -│   │   │   └── ioc -│   │   │   ├── common -│   │   │   │   └── beans -│   │   │   │   └── UserBean.java -│   │   │   └── README.MD -│   │   └── resources -│   │   └── config.xml -│   └── test -│   └── java -│   └── com -│   └── learn -│   └── note -│   └── practice -│   └── ioc -│   └── common -│   └── beans -│   └── UserBeanTest.java - -``` - -**NOTE:**关于在idea中把文件夹标注为制定文件夹根目录的方法,在文件夹上`右键->Mark Directory As` -具体有四个选项,分别是 -* `Sources Root` 源代码java文件根目录,本例中`src/main/java`文件夹就是默认的 -* `Test Sources Root` 测试代码根目录,本例中`test/java`目录是 -* `Resources Root`资源文件根目录,本例中的`resources`目录 -* `Excluded`编译的类`.class`文件根目录,本例中是`target`,对于所有`java`目录下的资源,编译时都会拷贝到对应的`Excluded`文件夹下 - -我们可以在`JavaPracticeCode.iml`文件中看到被标记的情况 -```xml - - - - - - - - - - - - - - - - - - - - - - - - - - - - -``` -注意这几行 -```xml - - - - - - -``` -以后如果发现单元测试无法创建或者运行,或者资源文件加载不上,自行加标记。 -单元测试的输出结果如下: -> 00:20:20.204 [main] INFO c.l.n.p.ioc.common.beans.UserBean - UserBean{userName='Fuck', userPassword='hehehe'} \ No newline at end of file diff --git a/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java b/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java deleted file mode 100644 index c9bb43c..0000000 --- a/src/main/java/com/learn/note/practice/ioc/common/beans/UserBean.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.learn.note.practice.ioc.common.beans; - -/** - * @author junqiangshen - * @version v1.0. - * @Time Created on 15-8-6. - */ -public class UserBean { - - private String userName; - private String userPassword; - - public UserBean() { - } - - public UserBean(String userName, String userPassword) { - - this.userName = userName; - this.userPassword = userPassword; - } - - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - public String getUserPassword() { - return userPassword; - } - - public void setUserPassword(String userPassword) { - this.userPassword = userPassword; - } - - @Override - public String toString() { - return "UserBean{" + - "userName='" + userName + '\'' + - ", userPassword='" + userPassword + '\'' + - '}'; - } -} diff --git a/src/main/resources/config.xml b/src/main/resources/config.xml deleted file mode 100644 index 0e91925..0000000 --- a/src/main/resources/config.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java b/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java deleted file mode 100644 index f0246e2..0000000 --- a/src/test/java/com/learn/note/practice/ioc/common/beans/UserBeanTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.learn.note.practice.ioc.common.beans; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import static org.junit.Assert.*; - -public class UserBeanTest { - private static Logger logger = LoggerFactory.getLogger(UserBean.class); - - @Test - public void testUserBeanSpring() { - ApplicationContext applicationContext = - new ClassPathXmlApplicationContext(new String[] {"config.xml"}); - // 取得一个实例 - UserBean user = (UserBean) applicationContext.getBean("userBean"); - logger.info(user.toString()); - } - -} \ No newline at end of file From 44e5321c21efad7d87cc51d00f7ab9b96ee16d5f Mon Sep 17 00:00:00 2001 From: sjq597 Date: Mon, 31 Aug 2015 23:30:59 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E5=85=88=E5=86=99=E4=B8=80=E4=B8=AAcontr?= =?UTF-8?q?oller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-bbs-demo/spring-bbs-demo.iml | 52 ------------------ .../springbbs/controller/LoginController.java | 53 +++++++++++++++++++ .../com/springbbs/domain/LoginCommand.java | 38 +++++++++++++ 3 files changed, 91 insertions(+), 52 deletions(-) delete mode 100644 spring-bbs-demo/spring-bbs-demo.iml create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/controller/LoginController.java create mode 100644 spring-bbs-demo/src/main/java/com/springbbs/domain/LoginCommand.java diff --git a/spring-bbs-demo/spring-bbs-demo.iml b/spring-bbs-demo/spring-bbs-demo.iml deleted file mode 100644 index 08d9772..0000000 --- a/spring-bbs-demo/spring-bbs-demo.iml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-bbs-demo/src/main/java/com/springbbs/controller/LoginController.java b/spring-bbs-demo/src/main/java/com/springbbs/controller/LoginController.java new file mode 100644 index 0000000..a1c1b5c --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/controller/LoginController.java @@ -0,0 +1,53 @@ +package com.springbbs.controller; + +import com.springbbs.domain.LoginCommand; +import com.springbbs.domain.User; +import com.springbbs.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import java.util.Date; + +/** + * @Description JavaPracticeCode + * Created by junqiangshen on 15-8-31. + */ +@Controller //标注成为一个Spring MVC的Controller +public class LoginController { + + @Autowired + private UserService userService; + + /** + * 负责处理/index.html的请求 + */ + @RequestMapping(value = "/index.html") + public String loginPage() { + return "login"; + } + + /** + * 登陆验证视图 + * @param loginCommand 扥路信息 + */ + @RequestMapping(value = "/loginCheck.html") + public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) { + boolean isValidUser = userService.hasMatchUser( + loginCommand.getUserName(), loginCommand.getPassword()); + + if (!isValidUser) { + return new ModelAndView("login", "error", "用户名或密码错误"); + } else { + User user = userService.findUserByUserName(loginCommand.getUserName()); + user.setLastIp(request.getLocalAddr()); + user.setLastVistit(new Date()); + userService.loginSucess(user); + + request.getSession().setAttribute("user", user); + return new ModelAndView("main"); + } + } +} diff --git a/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginCommand.java b/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginCommand.java new file mode 100644 index 0000000..ea330ea --- /dev/null +++ b/spring-bbs-demo/src/main/java/com/springbbs/domain/LoginCommand.java @@ -0,0 +1,38 @@ +package com.springbbs.domain; + +import java.io.Serializable; + +/** + * @Description JavaPracticeCode + * Created by junqiangshen on 15-8-31. + */ +public class LoginCommand implements Serializable{ + private static final long serialVersionUID = 1556475033994145521L; + + private String userName; + private String password; + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "LoginCommand{" + + "userName='" + userName + '\'' + + ", password='" + password + '\'' + + '}'; + } +} From c635e942f49a7a212682bcc605d63bda4c69409f Mon Sep 17 00:00:00 2001 From: sjq597 Date: Tue, 15 Sep 2015 23:42:31 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=B7servlet?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=92=8C=E5=86=99=E5=A5=BDcontroller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 + spring-bbs-demo/README.MD | 355 +++++------------- spring-bbs-demo/pom.xml | 6 + .../main/webapp/WEB-INF/springbbs-servlet.xml | 16 + .../src/main/webapp/WEB-INF/web.xml | 33 +- 5 files changed, 150 insertions(+), 268 deletions(-) create mode 100644 spring-bbs-demo/src/main/webapp/WEB-INF/springbbs-servlet.xml diff --git a/pom.xml b/pom.xml index ebf54d4..2bd7c0a 100644 --- a/pom.xml +++ b/pom.xml @@ -119,6 +119,14 @@ commons-dbcp ${dhcp.version} + + + + javax.servlet + javax.servlet-api + 3.1.0 + + diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index 6cc6731..bc83928 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -8,284 +8,111 @@ MySQL:5.5.44 Tomcat:7.0.47 ​Maven:3.0.5 -#### 具体步骤 -* 新建一个Webapp工程,名字就叫spring-bbs-demo -项目的目录结构如下: -``` -├── pom.xml -├── README.MD -├── spring-bbs-demo.iml -└── src - └── main - ├── resources - └── webapp - ├── index.jsp - └── WEB-INF - └── web.xml - -``` -* 在mysql里创建两张表,数据库名为`sampledb` -```sql -DROP DATABASE IF EXISTS sampledb; -CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8mb4; -USE sampledb; - --- 创建用户表 -CREATE TABLE t_user ( - user_id INT AUTO_INCREMENT NOT NULL COMMENT '用户id', - user_name VARCHAR(30) NOT NULL DEFAULT '' COMMENT '用户名', - credits INT NOT NULL DEFAULT 0 COMMENT '论坛积分', - password VARCHAR(32) NOT NULL DEFAULT ''COMMENT '用户密码', - last_visit TIMESTAMP NOT NULL DEFAULT 0 COMMENT '最后访问时间', - last_ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '最后访问ip', - PRIMARY KEY (user_id) -)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户表'; - --- 创建用户登陆日志表 -CREATE TABLE t_login_log ( - login_log_id INT AUTO_INCREMENT NOT NULL COMMENT '日志id', - user_id INT NOT NULL DEFAULT 0 COMMENT '用户id', - ip VARCHAR(23) NOT NULL DEFAULT '0.0.0.0' COMMENT '访问ip', - login_datetime TIMESTAMP NOT NULL DEFAULT 0 COMMENT '访问时间', - PRIMARY KEY (login_log_id) -)ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '用户登陆日志表'; - -INSERT INTO t_user (user_name,password) VALUES ('admin','123456'); -``` - -#### 最终项目目录结构 - -``` -├── pom.xml -├── README.MD -├── spring-bbs-demo.iml -└── src - └── main - ├── java - │   └── com - │   └── springbbs - │   ├── dao - │   ├── domain - │   └── service - ├── resources - │   └── sql - │   └── user_table_init.sql - └── webapp - ├── index.jsp - └── WEB-INF - └── web.xml - -``` -**NOTE:**`java`为代码根目录,如果不是,可以在`java`文件夹上右击`->Mark Directory As->Resources Root`。 -同理,`resources`为资源根目录,`webapp`为`web`根目录,`test`目录下的`java`文件夹为`TestR Resources Root`目录 +#### 展现层 -#### 文件及文件夹相关函数 - -文件 | 解释 ------| -------------- -applicationContext.xml | Spring容器配置文件 -domain | 领域对象(实体类)存放文件夹,往往拥有对应的数据库表,一般要实现`Serializable`接口,以便序列化 -dao | 访问实体类的接口,一般一个实体类都会有一个dao与之对应,里面有一些对应的方法 -jdbcTemplate | Spring对jdbc的简单封装,可以轻松完成大部分的数据库操作而不必频繁的重复对数据库的打开,获取连接,查询,关闭等操作 -jdbcTemplate#query() | `query(String sql,Object[] args,RowCallbackHandler rch)`.第一个参数不解释了;第二个是占位符(?)对应的参数数据;第三个是查询结果的处理回调接口,该回调接口有一个方法`processRow(ResultSet resultSet)`负责将查询结果从`ResultSet`装载到类似于实例类对象的实例中。一般都是使用匿名内部类的方式来调用`RowCallbackHandler`接口 - -**NOTE:**在DAO文件中写的`sql`语句比较长,多行衔接要注意空格,不然会连在一起,具体小技巧是每一行最后加个空格。 - -### Spring中配置 -以上两个DAO实现类中并没有打开/和释放`Connection`,到底如何访问数据库呢?答案是`jdbc`被`spring`封装起来了,`JdbcTemplate`需要一个`dataSource`,从数据源中获取或返回连接。所以在`UserDao`和`LoginLogDao`中都提供了一个带`@Autowired`注解的`jdbcTemplate`对象。 -所以我们需要先申明一个数据源,然后再定义一个`JdbcTemplate Bean`,通过`Spring`的容器上下文自动绑定机制进行`Bean`的注入。配置文件在`Resources`文件夹下,名字叫`applicationContext.xml`,内容如下: +使用`Spring-MVC`,具体步骤如下: +先配置web.xml文件,在`webapp`的`WEB-INF`目录下 ```xml - - + + + + + + contextConfigLocation + classpath:applicationContext.xml + + + + + org.springframework.web.context.ContextLoaderListener + + + + + baobaotao + org.springframework.web.servlet.DispatcherServlet + 2 + + + + baobaotao + *.html + + + Archetype Created Web Application + - - - - - - - - - - - - ``` -##### 配置说明 -`jdbc.properties`文件为mysql配置文件,内容如下: -> -jdbc.driver=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/sampledb -jdbc.username=root -jdbc.password=root0724 - -在配置文件中加载mysql配置文件,在定义数据源时需要使用到配置文件中的信息 -#### 业务层 -在这个登陆实例中,仅有一个业务类,即`UserService`,它负责将持久层的`UserDao`和`LoginDao`组织起来完成用户/密码认证、登陆日志记录等操作。`loginSucess`将两个`DAO`组织起来共同完成一个事务性工作:更新两个表,虽然没有事务操作的影子,但是通过`Spring`事务配置即可。 -关于几个注解的解释: -注解 | 作用 ------|------ -`@Service` | 将`UserService`标注为一个服务层的`Bean` -`@Autowired` | 注入`userDao`和`loginLogDao`这两个`DAO`层的`Bean` - -#### 在Spring中装配Service -我们必须告诉`Spring`哪些业务类需要工作于事务环境下及事务的规则等内容,以便`Spring`根据这些信息自动为目标业务类添加事务管理功能。对`applicationContext.xml`更改: +配置文件 ```xml - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -``` -**NOTE:** 配置文件解释 -* 命名空间 -```xml - -xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" -``` - * Service注解扫描目录 - ```xml - - ``` -* 事务管理器 -```xml - -``` -* aop事务增强 -关于aop(面向切片的编程后面会细讲,目前可以理解为拦截和代理即可),`execution(* com.springbbs.service.. *(..))`的意思就是任意返回值的service包及子包下的任何参数的任何方法都切入进行事务管理。 -```xml - - - - - - - - - - - -``` - -#### 单元测试 -依赖 -```xml - - junit - junit - test - - - org.springframework - spring-test - test - + + baobaotao + org.springframework.web.servlet.DispatcherServlet + 2 + ``` -创建单元测试的方法:光标放在在需要测试的类名字上(本例中是`UserService`),按住`Ctrl+Shift+T`,`Junit4`单元测试,勾选3个方法即可。 -测试类`UserServiceTest`代码如下: +虽然这个servlet名字可以随意取,但是一般有个约定,就是还必须有一个-servlet.xml的配置文件,即在同目录下 +还需要有一个`baobaotao-servlet.xml`的Spring-mvc配置文件。不过不需要再定义在web.xml文件中,Spring MVC会自动将这些文件 +加载。 + +`*.html`指定拦截*.html的文件,一般喜欢使用*.do和*.action的后缀,但是使用.html后缀有几个好处。 +第一:用户不能根据url判断我们使用的是什么技术 +第二:*.html是静态网页后缀,可以骗过搜索引擎,增加被收录的概率 +同时对于那些真正的静态网页可以使用*.htm加以区分 + +Spring MVC +这个东西主要负责控制业务逻辑,即后台代码的逻辑页面跳转等控制,我们需要写一个Controller来进行控制 +先写一个`LoginController` ```java -package com.springbbs.service; - -import com.springbbs.domain.User; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import java.util.Date; - -@RunWith(SpringJUnit4ClassRunner.class) // 基于JUnit4的Spring测试框架 -@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //启动Spring容器 -public class UserServiceTest { +@Controller //标注成为一个Spring MVC的Controller 处理http请求 +public class LoginController { @Autowired private UserService userService; - @Test - public void testHasMatchUser() throws Exception { - boolean b1 = userService.hasMatchUser("admin", "123456"); - boolean b2 = userService.hasMatchUser("admin", "1111"); - - Assert.assertEquals(true, b1); - Assert.assertEquals(false, b2); + /** + * 负责处理/index.html的请求 + */ + @RequestMapping(value = "/index.html") + public String loginPage() { + return "login"; } - @Test - public void testFindUserByUserName() throws Exception { - User user = userService.findUserByUserName("admin"); - Assert.assertEquals("admin", user.getUserName()); - } - - @Test - public void testLoginSucess() throws Exception { - User user = userService.findUserByUserName("admin"); - user.setLastIp("127.0.0.1"); - user.setLastVistit(new Date()); - userService.loginSucess(user); + /** + * 登陆验证视图 + * @param loginCommand 扥路信息 + */ + @RequestMapping(value = "/loginCheck.html") + public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) { + boolean isValidUser = userService.hasMatchUser( + loginCommand.getUserName(), loginCommand.getPassword()); + + if (!isValidUser) { + return new ModelAndView("login", "error", "用户名或密码错误"); + } else { + User user = userService.findUserByUserName(loginCommand.getUserName()); + user.setLastIp(request.getLocalAddr()); + user.setLastVistit(new Date()); + userService.loginSucess(user); + + request.getSession().setAttribute("user", user); + return new ModelAndView("main"); + } } } ``` -**NOTE:**`Spring`测试框架可以和`Junit4`整合,通过`Junit4`的`@RunWith`注解指定`SpringJUnit4ClassRunner.class`的测试运行器,该运行器是Spring提供的,可以将`Spring`容器和`Junit4`测试框架整合。`@ContextConfiguration`也是`Spring`提供的注解,它用于制定`Spring`的配置文件。 -这里需要注意的是,因为我的resources文件夹被指定为资源根目录,所以使用的 `classpath`路径来加载,即最终在类的根目录下。 -在`UserServiceTest`上右键->`Run UserServiceTest`即可运行单元测试。最后取数据库中查询登陆日志可以发现成功插入一条记录。 \ No newline at end of file +**NOTE:** `@Controller`标注的类也是一个`Bean`,所以在Controller里进行注入,控制器中可以映射多个不同的http请求路径,通过 +`@RequestMapping`指定路径。其中请求的参数会自动的绑定到响应方法的入参中,这个绑定是按照名字来绑定的,请求响应方法可以返回一个 +`ModelAndView`或者字符串,然后`Spring MVC`会自动解析做相应跳转。并且`ModelAndView`对象不仅包括了视图信息,也包含了渲染所需模型数据 + +#### Spring MVC配置文件 +光写一个`Controller`还不够,还需要在`springbbs-servlet.xml`文件中申明我们写的控制器,扫描web路径,指定`Spring MVC`的 +视图解析器,代码如下: +```xml + +``` \ No newline at end of file diff --git a/spring-bbs-demo/pom.xml b/spring-bbs-demo/pom.xml index f565980..ddf440d 100644 --- a/spring-bbs-demo/pom.xml +++ b/spring-bbs-demo/pom.xml @@ -89,6 +89,12 @@ commons-dbcp commons-dbcp + + + + javax.servlet + javax.servlet-api + diff --git a/spring-bbs-demo/src/main/webapp/WEB-INF/springbbs-servlet.xml b/spring-bbs-demo/src/main/webapp/WEB-INF/springbbs-servlet.xml new file mode 100644 index 0000000..0aa1383 --- /dev/null +++ b/spring-bbs-demo/src/main/webapp/WEB-INF/springbbs-servlet.xml @@ -0,0 +1,16 @@ + + + + + + + + + \ No newline at end of file diff --git a/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml b/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml index 9f88c1f..c1637e6 100644 --- a/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml +++ b/spring-bbs-demo/src/main/webapp/WEB-INF/web.xml @@ -1,7 +1,32 @@ - + + + + + + contextConfigLocation + classpath:applicationContext.xml + + + + + org.springframework.web.context.ContextLoaderListener + + + + + springbbs + org.springframework.web.servlet.DispatcherServlet + 2 + + + + springbbs + *.html + - Archetype Created Web Application From bf4f0bd2b4cffe4aec002e880e803c116b89b5cf Mon Sep 17 00:00:00 2001 From: sjq597 Date: Wed, 16 Sep 2015 23:35:12 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E5=90=8E=E7=AB=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-bbs-demo/README.MD | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index bc83928..cfb1856 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -114,5 +114,22 @@ public class LoginController { 光写一个`Controller`还不够,还需要在`springbbs-servlet.xml`文件中申明我们写的控制器,扫描web路径,指定`Spring MVC`的 视图解析器,代码如下: ```xml - -``` \ No newline at end of file + + + + + + + + + +``` +Spring MVC如何将视图逻辑名解析成具体视图?上面的bean即定义解析规则,可以有多种选择,这里使用的是`InternalResourceViewResolver` +它的工作方式就是为视图逻辑名添加前后缀的方式进行解析。举个例子,逻辑名为`login`,对应的解析为`/WEB-INF/jsp/login.jsp` \ No newline at end of file From 4c6464928dbe3c6c7b8ca2363ac86f913ca0697e Mon Sep 17 00:00:00 2001 From: sjq597 Date: Tue, 22 Sep 2015 23:05:01 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BA=E5=9D=9B?= =?UTF-8?q?=E7=9A=84=E5=9F=BA=E6=9C=AC=E7=99=BB=E9=99=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 ++++++++++- spring-bbs-demo/README.MD | 10 ++++++- spring-bbs-demo/pom.xml | 13 ++++++++ .../src/main/webapp/WEB-INF/jsp/login.jsp | 30 +++++++++++++++++++ .../src/main/webapp/WEB-INF/jsp/main.jsp | 17 +++++++++++ spring-bbs-demo/src/main/webapp/index.jsp | 2 +- 6 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 spring-bbs-demo/src/main/webapp/WEB-INF/jsp/login.jsp create mode 100644 spring-bbs-demo/src/main/webapp/WEB-INF/jsp/main.jsp diff --git a/pom.xml b/pom.xml index 2bd7c0a..4d549ca 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,8 @@ 1.8.0 5.1.34 1.2.2 + 3.1.0 + 1.1.2 @@ -124,7 +126,20 @@ javax.servlet javax.servlet-api - 3.1.0 + ${servlet.version} + + + + + jstl + jstl + ${jstl.version} + + + + taglibs + standard + ${jstl.version} diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index cfb1856..20fd5a5 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -132,4 +132,12 @@ public class LoginController { ``` Spring MVC如何将视图逻辑名解析成具体视图?上面的bean即定义解析规则,可以有多种选择,这里使用的是`InternalResourceViewResolver` -它的工作方式就是为视图逻辑名添加前后缀的方式进行解析。举个例子,逻辑名为`login`,对应的解析为`/WEB-INF/jsp/login.jsp` \ No newline at end of file +它的工作方式就是为视图逻辑名添加前后缀的方式进行解析。举个例子,逻辑名为`login`,对应的解析为`/WEB-INF/jsp/login.jsp`。 + +### JSP视图页面 +两个页面分别为登陆页面`login.jsp`以及`main.jsp`。 + +* login.jsp +```html + +``` \ No newline at end of file diff --git a/spring-bbs-demo/pom.xml b/spring-bbs-demo/pom.xml index ddf440d..9ba5ee6 100644 --- a/spring-bbs-demo/pom.xml +++ b/spring-bbs-demo/pom.xml @@ -94,7 +94,20 @@ javax.servlet javax.servlet-api + 3.1.0 + + + + jstl + jstl + + + + taglibs + standard + + diff --git a/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/login.jsp b/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/login.jsp new file mode 100644 index 0000000..42a63f7 --- /dev/null +++ b/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/login.jsp @@ -0,0 +1,30 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: junqiangshen + Date: 15-9-16 + Time: 下午11:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Spring样例演示论坛登陆界面 + + + + + + +
" method="post"> + 用户名: + +
+ 密码: + +
+ + +
+ + diff --git a/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/main.jsp b/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/main.jsp new file mode 100644 index 0000000..5bba758 --- /dev/null +++ b/spring-bbs-demo/src/main/webapp/WEB-INF/jsp/main.jsp @@ -0,0 +1,17 @@ +<%-- + Created by IntelliJ IDEA. + User: junqiangshen + Date: 15-9-16 + Time: 下午11:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + Spring 测试论坛 + + + ${user.userName},欢迎进入Spring 样例论坛,您当前积分为${user.credits}; + + diff --git a/spring-bbs-demo/src/main/webapp/index.jsp b/spring-bbs-demo/src/main/webapp/index.jsp index c38169b..4d05d28 100644 --- a/spring-bbs-demo/src/main/webapp/index.jsp +++ b/spring-bbs-demo/src/main/webapp/index.jsp @@ -1,5 +1,5 @@ -

Hello World!

+

Hello World test!

From 65550249eca7583f887f634017255c98a6e57729 Mon Sep 17 00:00:00 2001 From: sjq597 Date: Tue, 22 Sep 2015 23:28:28 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E5=A2=9E=E5=8A=A0README.MD=E8=AF=B4?= =?UTF-8?q?=E6=98=8E=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-bbs-demo/README.MD | 52 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index 20fd5a5..00ca9fa 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -138,6 +138,56 @@ Spring MVC如何将视图逻辑名解析成具体视图?上面的bean即定义 两个页面分别为登陆页面`login.jsp`以及`main.jsp`。 * login.jsp -```html +```jsp +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- + Created by IntelliJ IDEA. + User: junqiangshen + Date: 15-9-16 + Time: 下午11:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Spring样例演示论坛登陆界面 + + + + + + +
" method="post"> + 用户名: + +
+ 密码: + +
+ + +
+ + +``` +登陆成功界面 +```jsp +<%-- + Created by IntelliJ IDEA. + User: junqiangshen + Date: 15-9-16 + Time: 下午11:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + Spring 测试论坛 + + + ${user.userName},欢迎进入Spring 样例论坛,您当前积分为${user.credits}; + + ``` \ No newline at end of file From cb680d03e4e2bb899ce6f0011fbaee4a0dd74714 Mon Sep 17 00:00:00 2001 From: LittleQ Date: Tue, 22 Sep 2015 23:31:28 +0800 Subject: [PATCH 10/10] Update README.MD MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改排版,换行 --- spring-bbs-demo/README.MD | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-bbs-demo/README.MD b/spring-bbs-demo/README.MD index 00ca9fa..b917db4 100644 --- a/spring-bbs-demo/README.MD +++ b/spring-bbs-demo/README.MD @@ -1,12 +1,12 @@ ### 用spring实现一个论坛基本功能 #### 运行环境 -Linux:Ubun 14.04 64bit -IDE:IntelliJ IDEA 14.03 -JDK:1.7.40 -MySQL:5.5.44 -Tomcat:7.0.47 -​Maven:3.0.5 +Linux:Ubun 14.04 64bit +IDE:IntelliJ IDEA 14.03 +JDK:1.7.40 +MySQL:5.5.44 +Tomcat:7.0.47 +​Maven:3.0.5 #### 展现层 @@ -119,7 +119,10 @@ public class LoginController { xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:contex="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context.xsd"> @@ -190,4 +193,4 @@ Spring MVC如何将视图逻辑名解析成具体视图?上面的bean即定义 ${user.userName},欢迎进入Spring 样例论坛,您当前积分为${user.credits}; -``` \ No newline at end of file +```