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")根据名字装配
运行无误
上一篇: 保胎吃什么食物最好,有哪些禁忌
下一篇: 用正则按最后一个"_"分割"字符