精通Spring4.0

第二章 快速入门

2.2.2创建库表

(1)启动mysql数据库mysql -u root -p~~~

(2)创建实例对应的数据库sampledb

mysql> drop database if exists sampledb
    -> ;
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> create database sampledb default character set utf8;
Query OK, 1 row affected, 1 warning (0.14 sec)

mysql> use sampledb;
Database changed

(3)创建实例所用两张表

用户表

mysql> create table t_user(
    -> user_id int auto_increment primary key,
    -> user_name varchar(50),
    -> credits  int,
    -> password varchar(32),
    -> last_visit datetime,
    -> last_ip varchar(23)
    -> )engine=innodb;
Query OK, 0 rows affected (0.57 sec)

用户登陆日志表

mysql> create table t_login_log(
    -> login_log_id int auto_increment primary key,
    -> user_id int,
    -> ip varchar(23),
    -> login_datetime datetime
    -> )engine=innodb;
Query OK, 0 rows affected (0.41 sec)
其中mysql默认的引擎是myisam,不支持事务,只能存储数据,优点是读写速度快,但是innodb引擎则支持事务。

(4)初始化一条数据

mysql> insert into t_user(user_name,password) values('admin','123456')
    -> ;
Query OK, 1 row affected (0.17 sec)

mysql> commit;(这是关于事务的提交,但是输入上条就已经完成了,应该是要配合事务开始使用吧)

可以通过运行脚本完成上述工作,具体就是

-----a将上述命令写成sampledb.sql文件,然后cmd窗口输入mysql -u root -p ~~~ --port3306

d:\``````\sampled.sql

-----b这是第二种运行脚本的方法,直接用source命令运行

source d:\`````````\sampled.sql

2.2.3建立工程

用idea专业版直接创建了maven工程,然后开始配置Spring模块类库,依赖的数据库驱动类库,依赖的连接池类库,依赖的web类库(servlet-api,要装common-dbcp)

查看servlet版本号,直接打开了tomcat下lib里面的servlet-api.jar包,解压缩找WEB-INF下面的配置。晕,配置直接复制粘贴了,自己写太多。

出了个Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project chapter2: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

原来是maven自动寻找项目文件名webapps,但是idea生成的是web,所以找不到了。

在pom.xml里面添加

 

<webAppSourceDirectory>
    ${basedir}/web
</webAppSourceDirectory>

可以参考http://www.importnew.com/17936.html还有jetty的文档

还在code下面创建了一个文件夹web,好吧。。然后clean和install好了。

创建持久层

(1)创建User和LoginLog的domain对象

alter + insert来生成set和get方法,然后alt + enter导入包

(2)UserDao包括三个方法

getMatchCount()根据用户名和密码获取匹配的用户数

findUserByUserName()根据用户名获取User对象

updateLoginInfo()更新用户积分,最后登录IP和最后登陆时间

Spring2.5之后可以用注解的方式来定义Bean

(3)LoginLog包括一个方法

insertLoginLog()

2.3.4在Spring中装配DAO

jdbcTemplate本身封装了Connection等的操作,需要一个DataSource,这样就可以根据需要从DataSource获取或返回连接。UserDao和LoginLog都提供了一个带@Autowired注解的jdbcTemplate变量,所以必须声明一个数据源,然后定义一个JdbcTemplate Bean,通过Spring容器的上下文自动绑定机制,进行Bean的注入。

@Repository通过Spring注解定义一个DAO

@Autowired注入JdbcTemplate的Bean

总之,bean就是DAO为了和数据库连接的模块化类,更好通过反射实现操作,而domain则是用户定义的在各个业务层转发的。

2.4.3单元测试

直接照抄的,并没太理解,@ContextConfiguration,@Test,@Autowired以后分析吧

2.5展现层

找了半天才在project structure里面找到创建WEB-INF的,然后才有web.xml文件。如此,开始配置

(1)通过web容器上下文参数指定Spring配置文件的地址,从类路径下加载Spring配置文件

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:smart-context.xml</param-value>
</context-param>

(2)指定Spring所提供的ContextLoaderListener的web容器监听器,web容器启动时自动运行,会根据contextConfigLocation的web容器参数获取Spring配置文件,并启动Spring容器。要将log4j.properties日志配置文件放在该路径下,以便日志引擎自动生效。

SpringMVC也是和Struts一样,通过一个Servlet来拦截url请求

2.5.2处理登陆的请求

 

跑起来的时候我在pom.xml里面加了jdk选项

<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>

发现错误,用jetty:run运行没有错误,但是访问jsp页面立即报错。

HTTP ERROR 500

Problem accessing /bbs/index.html. Reason:

    PWC6033: Unable to compile class for JSP

PWC6199: Generated servlet error:
The type java.lang.Class cannot be resolved. It is indirectly referenced from required .class files

PWC6199: Generated servlet error:
java.util.Vector cannot be resolved to a type

PWC6199: Generated servlet error:
_jspx_dependants cannot be resolved

 

Caused by:

org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP

PWC6199: Generated servlet error:
The type java.lang.Class cannot be resolved. It is indirectly referenced from required .class files

PWC6199: Generated servlet error:
java.util.Vector cannot be resolved to a type

PWC6199: Generated servlet error:
_jspx_dependants cannot be resolvedhttps://blog.csdn.net/qq_36850813/article/details/79949925参考这个,把jstl的包给导入了,但是仍然报错。

在网站https://stackoverflow.com/questions/32803427/pwc6033-unable-to-compile-class-for-jsp找到

I had the same problem using Java 8 but using Java 7 works fine.

java -jar jackrabbit-standalone-2.11.3.jar

the jackrabbit-standalone-2.14.3.jar didn't work on jdk 8 , but it works file on jdk 7

直接找到网站上https://issues.apache.org/jira/browse/JCR-4183

我直接把java10卸载,改成了java8。卧槽,又出现版本问题

https://blog.csdn.net/codejas/article/details/79645663这个用Springboot后,加了parent可以不加其他依赖的版本号

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值