spring-mybatis.xml 访问html5,Spring mvc无xml配置及利用JdbcTemplate访问数据库
项目结构
一、新建动态web项目取名HelloSpringMVC
二、/WebContent/WEB-INF/lib下导入必要依赖库
commons-collections4-4.1.jar、commons-dbcp2-2.1.1.jar、commons-logging-1.2.jar
commons-pool2-2.4.3.jar、jstl-1.2.jar、mybatis-3.4.5.jar、mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.44-bin.jar、spring-aop-4.3.12.RELEASE.jar
spring-aspects-4.3.12.RELEASE.jar、spring-beans-4.3.12.RELEASE.jar
spring-context-4.3.12.RELEASE.jar、spring-context-support-4.3.12.RELEASE.jar
spring-core-4.3.12.RELEASE.jar、spring-expression-4.3.12.RELEASE.jar
spring-instrument-4.3.12.RELEASE.jar、spring-instrument-tomcat-4.3.12.RELEASE.jar
spring-jdbc-4.3.12.RELEASE.jar、spring-jms-4.3.12.RELEASE.jar
spring-messaging-4.3.12.RELEASE.jar、spring-orm-4.3.12.RELEASE.jar
spring-oxm-4.3.12.RELEASE.jar、spring-test-4.3.12.RELEASE.jar、spring-tx-4.3.12.RELEASE.jar
spring-web-4.3.12.RELEASE.jar、spring-webmvc-4.3.12.RELEASE.jar
spring-webmvc-portlet-4.3.12.RELEASE.jar、spring-websocket-4.3.12.RELEASE.jar
三、WebInitializer(相当于web.xml)
package com.hello.annotation;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// TODO 自动生成的方法存根
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AnnoConfig.class);
ctx.setServletContext(servletContext);//新建WebApplicationContext,注册配置类,并将其和当前的servletContext关联
//ctx.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
//注册sprigmvc的DispatcherServlet
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
四、AnnoConfig(相当于application.xml)这里把数据源也配置了
package com.hello.annotation;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan("com.hello.*")
public class AnnoConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean(name = "primaryDataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/test2?useSSL=false");
ds.setUsername("your-user");
ds.setPassword("your-passwd");
return ds;
}
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
五、控制类
package com.hello.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
@RequestMapping("/")
public String sayHello(ModelMap model) {
model.addAttribute("greeting", "你好! Spring 4 MVC");
return "welcome";
}
@RequestMapping("/login")
public String login(HttpSession session, String username, String password) throws Exception {
// 调用 service 层验证登录
// ...
// 模拟登录成功
session.setAttribute("username", username);
return "redirect:/u/list";
}
}
六、视图welcome.jsp(放在WEB-INF/views/下)
pageEncoding="UTF-8"%>
在此处插入标题
Greeting : ${greeting}
七、运行
八、JdbcTemplate访问数据库部分
IUser、User、UserRowMapper、UserRepository等类和接口完全参照SpringBoot的dao层、JdbcTemplate多数据源访问实例。这里只把包com.yiibai改为com.hello。
增加控制类Ucontroller也是参考SpringBoot的dao层、JdbcTemplate多数据源访问实例。但修改较大(用了ModelAndView),具体内容如下:
package com.hello.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.hello.mybatis.models.User;
import com.hello.mybatis.repositories.UserRepository;
@Controller
@RequestMapping("/u")
public class Ucontroller {
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate primaryJdbcTemplate;
@Autowired
private UserRepository userRepository;
@RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
public ModelAndView getUsers() {
List ulist=userRepository.findAll();
ModelAndView modelAndView = new ModelAndView("userslist");//转到/WEB-INF/views/userslist.jsp
//modelAndView.setViewName("userslist");
modelAndView.addObject("userslist", ulist);
return modelAndView;
}
}
这里要注意应该引入的是: org.springframework.web.servlet.ModelAndView;
而不是 org.springframework.web.portlet.ModelAndView,否则会出错。
视图userslist.jsp(也在WEB-INF/views/下)
pageEncoding="UTF-8"%>
在此处插入标题
编号 | 名字 | 部门 | 电话 |
---|---|---|---|
${users.id} | ${users.name} | ${users.dept} | ${users.phone} |
运行:
http://localhost:8080/HelloSpringMVC/login
HelloWorldController中"/login"重定向到"/u/list",然后调用Ucontroller类中public ModelAndView getUsers()。
项目代码点击:下载
参考:
spring-mybatis.xml 访问html5,Spring mvc无xml配置及利用JdbcTemplate访问数据库相关推荐
- Spring MVC 无XML配置入门示例
Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...
- Spring Integration 4.0:完整的无XML示例
1.简介 Spring Integration 4.0终于发布了 ,并且此版本具有非常好的功能. 本文介绍的一种可能性是完全不使用XML即可配置集成流程. 那些不喜欢XML的人仅使用JavaConfi ...
- idea+springmvc+spring+mybatis+maven整合返回json数据web api-
本人大三,第一次写博客,还有许多不懂得地方,如果有不当的地方 欢迎各位指教 项目地址:https://github.com/qq571831158/Springmvc-spring-mybatisDe ...
- idea springmvc_SSM三大框架使用Maven快速搭建整合(SpringMVC+Spring+Mybatis)
本文介绍使用SpringMVC+Spring+MyBatis三大框架使用Maven快速搭建一个demo,实现数据从数据库中查询返回到页面进行展示的过程. 技术选型:SpringMVC+Spring+M ...
- spring + mybatis + c3p0 整合(配置篇)
摘要 近期由于项目使用mybatis出现了数据源阻塞,导致应用程序假死,服务超时引发严重后果,故此下定决心重新梳理一下spring+mybatis+c3p0整合问题,主要分为:配置.源码(通过一次数据 ...
- spring Mybatis注解加判断
spring Mybatis注解加判断 spring Mybatis注解加判断 一.用script标签包围,然后像xml语法一样书写 判断参数是否为空或者为null,没有参数则不执行该语句 @Sele ...
- Spring MyBatis Atomikos 实现JTA分布式事务
Spring+MyBatis+Atomikos实现JTA分布式事务 项目中需要同时操作两个数据库,对两个数据库中的表同时做变更时就需要控制事务,要么全部成功,要么全部失败. Atomikos是一个开源 ...
- php hincrby,如何利用PHP访问带有密码的Redis
如何利用PHP访问带有密码的Redis 导语:如何利用PHP访问带有密码的Redis方法,下面小编给大家提供了代码实现步骤,大家可以参考阅读,更多详情请关注应届毕业生考试网. 一.首先设置Redis密 ...
- Spring MVC之基于java config无xml配置的web应用构建
更多spring相关博文参考: spring.hhui.top 前一篇博文讲了SpringMVC+web.xml的方式创建web应用,用过SpringBoot的童鞋都知道,早就没有xml什么事情了,其 ...
最新文章
- HTC VIVE 虚拟现实眼镜VR游戏体验
- Git中pull,commit和push的概念
- VS2010问题汇总
- 操作系统实验报告-系统调用
- VMProtect Ultimate 加壳脱壳工
- 中国移动CMPP接口
- 【AI视野·今日Robot 机器人论文速览 第二十三期】Tue, 28 Sep 2021
- node mysql菜鸟教程_Node.js 创建第一个应用
- cesium加载天地图的卫星影像图
- 北航大学计算机学院新媒体艺术系,本科优秀毕业论文参考-北航新媒体与艺术学院-北京航空航天大学.doc...
- 单片机无线调试-看见心跳-手机显示心率波形
- 西瓜中视频是如何赚钱的?教你提高视频收益的3个方法!
- poi导出excel写入公式_【java poi 写入Excel后读取公式值问题】poi导入excel
- vue 使用emoji表情包
- windows计算机图标历史,从1993年至今,Windows开始菜单的传奇发展史
- 简单理解数字签名和验签
- 自考本计算机应用免试,惊喜!自考计算机科目可以免试?
- R5S OpenWrt下smba共享文件夹
- java -jar 参数说明
- my fav html
热门文章
- linux启动报错+centos关闭和开启ipv6
- [云炬创业管理笔记]第四章把握创业机会测试6
- 动态服务器值 回放报错 没有关联到_性能测试每天两个知识点-web性能脚本回放不成功的解决方法...
- python 日志不会按照日期分割_python日志切割保留一个月
- 用存储过程还原数据库
- 经典问题:向setTimeout传递函数参数
- C语言使用fopen的两点注意事项
- IE 中释放javascript对象
- Delphi数据库编程一日通
- 使用脚本实现自动清除指定文件夹下丢失链接文件的符号链接