SSM!这就是你要的条条框框!
第一次写博
1、导jar包

2,、表和实体类
实体类:com.bao.entity【Student】
private int stuNo;
private String stuName;
private int stuAge;
//getting and setting
表:【student】
stuNo int stuName varchar stuAge int
3、建spring的配置文件【applicationContex.xml】
4、建mybatis映射文件【studentmapper.xml】
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bao.mapper.StudentMapper.xml">
<select id="queryStudentBystuNo" resultType="Student">
select * from student where stuNo = #{stuNo}
</select>
</mapper>
5、在【web.xml】中配置,将spring纳入web项目(可通过直接提示contextloaderlistener)
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
6、回到【applicationContex.xml】配置数据源(数据库信息)和mapper
<!-- 数据源 -->
<!-- 引入properties便于下面的数据源配置 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 配置数据库信息(代替了mybaties的配置文件conf.xml)-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置mybatis需要的核心类:sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mapper.xml路径 -->
<property name="mapperLocations" value="com/bao/mapper/*.xml"></property>
</bean>
<!-- spring整合mybatis:将mybatis的sqlsessionfactory交给spring -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 将这个包里面的接口产生对应的动态代理对象,对象名就是首字母小写的接口名 -->
<!-- 相当于StudentMapper studentMapper=session.getMapper(StudentMapper.class) -->
<property name="basePackage" value="com.bao.mapper"></property>
</bean>
逻辑:
用spring的方法生成mybatis的核心类sqlsessionfactory
引入上面的数据源,加载mapper.xml
最后将mybatis的sqlsessionfactory交给spring
sm整合完毕(别忘了在mapper.xml中写sql语句和在mapper.java创建接口)
7、在【web.xml】配置springmvc(可通过直接提示dispatherservlet)
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContex-controller.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern><!-- 拦截一切请求 --> </servlet-mapping>
并创建【applicationContex-controller.xml】(这是springmvc配置文件)
8、编写applicationContex-controller.xml
<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- springmvc基础配置 --> <mvc:annotation-driven></mvc:annotation-driven>
建com.bao.service【IStudentService】(接口)
建com.bao.impl【StudentServiceImpl】(实现类实现上面的接口)
建com.bao.controller【StudentController】(mvc控制器,所以要加@Controller注解)
在applicationContex-controller.xml添加扫描器扫描所有的控制器
<context:component-scan base-package="com.bao.controller"></context:component-scan>
编写控制器StudentController
@Controller
@RequestMapping("controller")
public class StudentController {
@RequestMapping("queryStudentByNo")
public String queryStudentByNo() {
}
}
创建并写一个前台jsp
<a href="controller/queryStudentByNo/1">查询1号学生</a>
通过ant风格传过来一个"1"的数据
控制器StudentController这样来接收
public class StudentController {
@RequestMapping("queryStudentByNo{stuno}")
public String queryStudentByNo(@PathVariable("stuno")int stuNo) {
}
}
Dao层写sql语句(StudentMapper.xml写语句、StudentMapper.java写接口)
Service调Dao
【IStudentService】
Student quetyStudentBuNo(int stuNo);
并在实现类【StudentServiceImpl】实现(或许不用写接口),引入Dao层的接口
StudentMapper studentmapper;
@Override
public Student quetyStudentBuNo(int stuNo) {
return null;
}
去springIOC容器为【StudentServiceImpl】注入StudentMapper类
(这是set注入,所以studentmapper要有setting,studentMapper处是com.bao.mapper【StudentMapper.java】的动态代理对象--类名首字母小写)
<bean id="studentService" class="com.bao.impl.StudentServiceImpl"> <property name="studentmapper" ref="studentMapper"></property> </bean>
所以【StudentServiceImpl】的studentmapper可以调用Dao的方法了
return studentmapper.queryStudentBystuNo(stuNo);
控制器依赖service(同样在springIOC容器注入,此步骤略)
IStudentService studentService;
现在控制器可以调用下层的方法并用Student接收,student放进map中,map会将这个对象放到request域中,最后跳转到"result"页面
@RequestMapping("queryStudentByNo{stuno}")
public String queryStudentByNo(@PathVariable("stuno")int stuNo,Map<String,Object> map) {
Student student=studentService.quetyStudentBuNo(stuNo);
map.put("student", student);
return "result";
}
【result.jsp】
${reqyestScope.student.stuNo}-${reqyestScope.student.stuName}-${reqyestScope.student.stuAge}
测试
错误:com/bao/mapper不存在
Could not resolve resource location pattern [com/bao/mapper/*.xml]: ServletContext resource [/com/bao/mapper/] cannot be resolved to URL because it does not exist
原因springmvc的影响
解决方法:classpath:com/bao/mapper
错误:不能从字符串转到studentmapper类型
Cannot convert value of type 'java.lang.String' to required type
'com.bao.mapper.StudentMapper' for property 'studentmapper': no matching editors or conversion strategy found
原因;
解决方法:依赖注入Dao哪里
value改成ref
成功启动:点击超链接会报500空指针错误
原因:控制器通过注解加入到IOC容器里,又用过配置加入到IOC容器里,矛盾报错
解决:注释掉IOC容器里关于控制器的注入,并在控制器里类里加自动装配@Autowired
@Qualifier("studentService")根据名字装配
运行无误
SSM!这就是你要的条条框框!的更多相关文章
- 【JAVA - SSM】之MyBatis与原生JDBC、Hibernate访问数据库的比较
首先来看一下原生JDBC访问数据库的代码: public static void main(String[] args) { // 数据库连接 Connection connection = null ...
- SSM手把手整合教程&测试事务
自打来了博客园就一直在看帖,学到了很多知识,打算开始记录的学习到的知识点 今天我来写个整合SpringMVC4 spring4 mybatis3&测试spring事务的教程,如果有误之处,还请 ...
- 基于Mysql数据库的SSM分页查询
前言: Hello,本Y又来了,"分页"在我们使用软件的过程中是一个很常见的场景,比如博客园对于每个博主的博客都进行了分页展示.可以简单清晰的展示数据,防止一下子将过多的数据展现给 ...
- MyEclipse的多模块Maven web(ssm框架整合)
Maven的多模块可以让项目结构更明确,提高功能的内聚,降低项目的耦合度,真正的体现出分层这一概念. 我们在操作中,要明白为什么这样做,要了解到更深的层次,这样,我们就不限于个别软件了. 话不多说,直 ...
- IDEA 整合 SSM 框架学习
认识 Spring 框架 更多详情请点击这里:这里 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control ...
- SSM项目整合Quartz
一.背景 SSM项目中要用到定时器,初期使用Timer,后来用spring 的schedule,都比较简单,所以功能比较单一而且他们不能动态的配置时间.后来就研究quartz,准备整合到项目中.Qua ...
- 关于利用maven搭建ssm的博客,我们一起来探讨下问的最多的问题
前言 开心一刻 有个同学去非洲援建,刚到工地接待他的施工员是个黑人,他就用英语跟人家交流,黑人没做声. 然后他又用法语,黑人还是没说话. 然后他用手去比划.黑人终于开口了:瞎比划嘎哈,整个工地都中国人 ...
- Java开发学习心得(一):SSM环境搭建
目录 Java开发学习心得(一):SSM环境搭建 1 SSM框架 1.1 Spring Framework 1.2 Spring MVC Java开发学习心得(一):SSM环境搭建 有一点.NET的开 ...
- Java归去来第4集:java实战之Eclipse中创建Maven类型的SSM项目
一.前言 如果还不了解剧情,请返回第3集的剧情 Java归去来第3集:Eclipse中给动态模块升级 二.在Eclipse中创建Maven类型的SSM项目 2.1:SSM简介 SSM ...
随机推荐
- JavaScript 声明全局变量与局部变量
一.JavaScript 声明全局变量的三种方式: 声明方式一: 使用var(关键字)+变量名(标识符)的方式在function外部声明,即为全局变量,否则在function声明的是局部变量.该方式即 ...
- swift 泛型的类型约束
总结: 1.类型约束只能添加到泛型参量上面 2.关联类型是泛型参量: 3.关联类型可以通过 协议.关联类型名称的形式引用: func allItemsMatch<C1: Container, C ...
- 获取Java接口的所有实现类
获取Java接口的所有实现类 前言:想看基于spring 的最简单实现方法,请直接看 第七步. 本文价值在于 包扫描的原理探究和实现 一.背景 项目开发中,使用Netty做服务端,保持长连接与客户端( ...
- tomcat https协议
一.tomcat证书 JDK自带的keytool工具来生成证书 1. 在jdk的安装目录\bin\keytool.exe下打开keytool.exe 2. 在命令行中输入以下命令: keytool - ...
- core 中使用 swagger
引包 代码 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().Set ...
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...
- sqlserver还原3101
1.出现错误"3101" 2.解决办法:删除数据库之后还原(有风险)或者获得数据库的独占访问权(用sql语句) 参考:https://www.2cto.com/database/2 ...
- jquery 拖动(Draggable) 约束运动,输出数组排序Array
<!doctype html><html lang="en"><head> <meta charset="utf-8" ...
- CodeForces - 1105D Kilani and the Game(多源BFS+暴力)
题目: 给出一张游戏地图和每个玩家的位置,每次能移动的步数.p个玩家轮流移动占领地图中的格子(当格子已经被占领时就不能在占领了)在每个玩家都不能移动时游戏结束. 问在游戏结束后,每个玩家占领的格子的数 ...
- Mybatis 头信息
在使用IDEA开发中,如果不使用Mybatis-Generator时,那么就需要手写Mapper文件,而在创建xml文件时无法直接创建带有mybatis头信息的mapper文件,这里来记录一下Myba ...