javascript
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访问数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何启用计算机超级账户,Windows7
- 下一篇: 斯蒂文斯理工学院计算机博士,美国斯蒂文斯