MyBatis学习---整合SpringMVC

【目录】

  😝新建maven项目

  😁搭建springMVC

  😎使用MyBatis连接数据库

    😊MyBatis整合SpringMVC

  😂MyBatis逆向工程

 

【jstl】

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。

JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。

 

【修改步骤】

①添加jstl加包,pop.xml

 1 <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
 2 <dependency>
 3         <groupId>javax.servlet.jsp.jstl</groupId>
 4         <artifactId>jstl-api</artifactId>
 5         <version>1.2</version>
 6     <exclusions>
 7         <exclusion>
 8             <groupId>javax.servlet</groupId>
 9             <artifactId>servlet-api</artifactId>
10         </exclusion>
11         <exclusion>
12              <groupId>javax.servlet.jsp</groupId>
13              <artifactId>jsp-api</artifactId>
14         </exclusion>
15     </exclusions>
16 </dependency>
17 <dependency>
18         <groupId>org.glassfish.web</groupId>
19         <artifactId>jstl-impl</artifactId>
20         <version>1.2</version>
21     <exclusions>
22         <exclusion>
23             <groupId>javax.servlet</groupId>
24             <artifactId>servlet-api</artifactId>
25         </exclusion>
26         <exclusion>
27             <groupId>javax.servlet.jsp</groupId>
28             <artifactId>jsp-api</artifactId>
29         </exclusion>
30         <exclusion>
31             <groupId>javax.servlet.jsp.jstl</groupId>
32             <artifactId>jstl-api</artifactId>
33         </exclusion>
34     </exclusions>
35 </dependency> 

 

②将SqlSession封装成工具类DBTools.java

 1 package com.springdemo.tools;
 2 
 3 import java.io.InputStream;
 4 import java.io.Reader;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 public class DBTools {
12     public static SqlSessionFactory sessionFactory;    
13     static{
14         try {
15             //使用MyBatis提供的Resources类加载mybatis的配置文件
16             InputStream reader = Resources.getResourceAsStream("conf.xml");
17             //构建sqlSession的工厂
18             sessionFactory = new SqlSessionFactoryBuilder().build(reader);
19         } catch (Exception e) {
20             e.printStackTrace();
21         }  
22     }
23     //创建能执行映射文件中sql的sqlSession
24     public static SqlSession getSession(){
25         return sessionFactory.openSession();
26     }
27     
28 }

 

🌂修改DemoController.java

 1 package com.springdemo.controller;
 2 import java.util.List;
 3 
 4 import javax.servlet.http.HttpServletRequest;
 5 import javax.servlet.http.HttpServletResponse;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 import org.springframework.web.servlet.ModelAndView;
10 
11 import com.springdemo.model.User;
12 import com.springdemo.tools.DBTools;
13 
14 @Controller
15 @RequestMapping("/demo")
16 public class DemoController {
17 
18 //    @RequestMapping("/index")
19 //    public String index(){
20 //        return "demo";
21 //    }
22     
23     @RequestMapping("/index")
24     public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){
25 
26         String stateselect = "com.springdemo.mapping.userMapper.selectUser";
27         List<User> userList = DBTools.getSession().selectList(stateselect);
28                 
29         System.out.println("users");
30         ModelAndView mav=new ModelAndView("demo");
31         mav.addObject("users",userList);
32         return mav;
33     }
34 }

 

④修改demo.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 
 4 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 5 
 6 <!DOCTYPE html>
 7 <html>
 8 <head>
 9 <meta charset="utf-8">
10 <title>DomePage</title>
11 </head>
12 <body>
13 
14 <h1>This is springMVC Demo Page</h1>
15 
16 <c:forEach items="${users}" var="user">  
17        用户名:${user.userName },用户密码:${user.userPwd }<br />
18 </c:forEach>
19 
20 </body>
21 </html>

 

😭Tomcat部署结果

  

 

posted @ 2019-02-14 22:00  茄子鱼  阅读(2629)  评论(0编辑  收藏  举报