ssm半路集成

Posted 我市小到中雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm半路集成相关的知识,希望对你有一定的参考价值。

1.eclipse下创建web项目

第一步:首先添加持久层框架mybaties

添加mybaties所需要的架包mybaties-3.4.4.jar,连接mysql数据库的驱动包mysql_con_java.jar

(1) 配置mybaties的配置文件,mybaties-cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 导入连接数据库的资源文件 ****.properties -->
<!-- <properties resource="baseDAO.properties">
</properties> -->
<typeAliases><!--
<typeAlias type="bean.User" alias="u"></typeAlias>
<typeAlias type="java.lang.String" alias="s"></typeAlias>
<typeAlias type="int" alias="i"></typeAlias>
-->
<!-- name的值直接写包名 -->
<package name="com.neusoft.bean"></package>
</typeAliases>


<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.gjt.mm.mysql.Driver" />
<property name="url" value="jdbc:mysql://localhost/my" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/neusoft/mapper/LoginMapper.xml" />
</mappers>
</configuration>

(2.)配置Mapper(mapper相当于实现LoginDAO接口,其中只包含查询的功能)

<?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.neusoft.dao.LoginDAO">
<!-- 查询全部学生信息 -->
<!-- select标签:查询 id的值一定是唯一不能重复的,
以后它是接口中的未实现的方法 ,resultType返回的类型或输出的类型,
结果类型可以是任意类型,但通过返回集合时返回的是list或实体类-->
<select id="selectUser" resultType="Login">
select * from login
</select>
</mapper>

(3.)创建接口com.neusoft.dao.LoginDAO,其中接口中的查询方法必须和mapper文件中select标签中的id一致

这样mybaties就大致配置完成,此时Sqlsession还未被打开

 

第二步:集成springMVC框架

1.   引入22个springmvc架包

2.   配置web.xml拦截器web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssmbyme</display-name>

<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名称 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.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>

<!-- 解决jquery引用不了的文题,因为所有的/路径都交给给了springmvc处理,不能处理静态,css,js -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.    配置springmvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.neusoft.controller"></context:component-scan>

<!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>

 

4.创建com.neusoft.controller中的被扫描的controller类

5.

编写login的controller

package com.neusoft.controller;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neusoft.dao.LoginDAO;

@Controller
public class Login {
@RequestMapping(value="login")
public String login(String username,String password){
String isright = null;
SqlSession sqlSession=null;
String resource = "mybaties-cfg.xml";
InputStream inputStream;
try {
//
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().
build(inputStream);

sqlSession=sqlSessionFactory.openSession();
System.out.println("ok");
LoginDAO mapper = sqlSession.getMapper(LoginDAO.class);
com.neusoft.bean.Login aaa = new com.neusoft.bean.Login();
aaa.setUsername(username);
aaa.setPassword(password);
com.neusoft.bean.Login list = mapper.selectUser(aaa);
if(list != null){
isright = "success";
}else{
isright = "no";

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isright;


}
}

6.编写主页面index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div style="text-align: center; ">
<p>welcome to login</p>

<form action="login">
<table>

<tbody>
<tr> <input type="text" name="username" /> </tr><br><br>
<tr> <input type="text" name="password" /> </tr><br>
<tr> <input type="submit" value="submit"/> </tr><br>
</tbody>
</table>
</form>
</div>

</body>
</html>

7.编写success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>welcome to login!</title>
</head>
<body>
<div style="text-align: center; ">
<p> welcome: <span style="color: red;">${uname}</span> to join </p>
</div>

</body>
</html>

7.编写no.jsp

 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>

<script type="text/javascript">
alert("2b!!!false to login @[email protected]");
window.location="index.jsp";
</script>
</body>
</html>

技术分享

以上是关于ssm半路集成的主要内容,如果未能解决你的问题,请参考以下文章

Maven集成SSM

ssm集成步骤

maven_SSM集成的demo

ssm集成mongodb

MyBatis&&ssm项目集成

Redis缓存 集成SSM框架