博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC+MyBatis+Spring 整合(2)
阅读量:5344 次
发布时间:2019-06-15

本文共 7994 字,大约阅读时间需要 26 分钟。

mybatis 与Spring 的整合.  

1.导入Spring 和Springmvc的包

pom

4.0.0
cn.mjorcen.prot
prot
0.0.1-SNAPSHOT
war
UTF-8
org.springframework
spring-test
3.2.0.RELEASE
org.springframework
spring-webmvc
3.2.0.RELEASE
org.springframework
spring-core
3.2.0.RELEASE
org.mybatis
mybatis
3.1.1
org.mybatis
mybatis-spring
1.1.1
mysql
mysql-connector-java
5.1.21
junit
junit
4.11
test
com.alibaba
druid
0.2.9
org.aspectj
aspectjweaver
1.7.1
org.codehaus.jackson
jackson-mapper-asl
1.9.11
commons-fileupload
commons-fileupload
1.2.2
javax.servlet
servlet-api
3.0-alpha-1
log4j
log4j
1.2.17
com.alibaba
fastjson
1.1.26
maven-war-plugin
maven-compiler-plugin
1.6
1.6

2. 添加spring-mvc.xml 文件

text/html;charset=UTF-8
UTF-8
32505856
4096

3.添加spring-application.xml 文件

4 . 修改web,xml 

mybatis
contextConfigLocation
classpath:spring-*.xml
字符集过滤器
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
字符集编码
encoding
UTF-8
encodingFilter
/*
spring监听器
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
spring mvc servlet
springMvc
org.springframework.web.servlet.DispatcherServlet
spring mvc 配置文件
contextConfigLocation
classpath:spring-mvc.xml
1
springMvc
*.do
/index.jsp
15

5. controller 如下

1 package com.sd.test.controller; 2  3 import java.io.IOException; 4  5 import javax.annotation.Resource; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 import org.springframework.stereotype.Controller;10 import org.springframework.web.bind.annotation.RequestMapping;11 12 import com.sd.test.model.TTest;13 import com.sd.test.service.TestService;14 15 @Controller16 @RequestMapping("/test")17 public class TestController {18     @Resource19     private TestService testService;20 21     @RequestMapping("selectOne")22     public void findById(HttpServletRequest request,23             HttpServletResponse response) throws IOException {24         String id = request.getParameter("id");25         Object obj = this.testService.selectOne(Integer.parseInt(id));26         response.getWriter().print(obj);27     }28 29     @RequestMapping("save")30     public void save(HttpServletRequest request, HttpServletResponse response) {31         String name = request.getParameter("name");32         TTest t = new TTest();33         t.setUsername(name);34         this.testService.save(t);35 36     }37 38     @RequestMapping("tsave")39     public void tsave(HttpServletRequest request, HttpServletResponse response) {40         String name = request.getParameter("name");41         TTest t = new TTest();42         t.setUsername(name);43         int i = (Integer) this.testService.put(t);44         System.out.println(i);45     }46 47 }

serviceImpl 如下:

package com.sd.test.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.sd.test.dao.TTestMapper;import com.sd.test.model.TTest;import com.sd.test.service.TestService;@Servicepublic class TestServiceImpl implements TestService {    // @Resource    // private TTestMapper mapper;    @Override    public void save(Object obj) {        // int i = this.mapper.insert((TTest) obj);        // System.out.println("save = " + i);        System.out.println("save");    }    @Override    public Object selectOne(Object obj) {        System.out.println("selectOne");        TTest tt = null;        // TTest tt = this.mapper.selectByPrimaryKey(Integer.parseInt(obj        // .toString()));        return tt;    }    @Override    public Object put(Object obj) {        System.out.println("put");        // return this.mapper.insert((TTest) obj);        return null;    }}

 

 

启动项目.访问http://localhost:8080/prot/test/selectOne.do?id=1

控制台:

selectOne

到此为止, spring mvc 部署成功.

 

转载于:https://www.cnblogs.com/mjorcen/p/3693161.html

你可能感兴趣的文章
[BZOJ2938] 病毒
查看>>
webstorm修改文件,webpack-dev-server不会自动编译刷新
查看>>
Scikit-learn 库的使用
查看>>
CSS: caption-side 属性
查看>>
python 用数组实现队列
查看>>
认证和授权(Authentication和Authorization)
查看>>
Mac上安装Tomcat
查看>>
CSS3中box-sizing的理解
查看>>
传统企业-全渠道营销解决方案-1
查看>>
Lucene全文检索
查看>>
awk工具-解析1
查看>>
推荐一款可以直接下载浏览器sources资源的Chrome插件
查看>>
CRM product UI里assignment block的显示隐藏逻辑
查看>>
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告...
查看>>
AMH V4.5 – 基于AMH4.2的第三方开发版
查看>>
Web.Config文件配置之配置Session变量的生命周期
查看>>
mysql导入source注意点
查看>>
linux下编译安装nginx
查看>>
ArcScene 高程不同的表面无法叠加
查看>>
[ONTAK2010] Peaks
查看>>