Spring SpringMVC Mybatis 整合

来源:互联网 发布:人工智能觉醒 编辑:程序博客网 时间:2024/06/08 10:46

1.首先导入所需的 jar 包

2.写三层

model层

package com.sm.model;public class userInfo {private String username;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpwd() {return userpwd;}public void setUserpwd(String userpwd) {this.userpwd = userpwd;}public String getUserid() {return userid;}public void setUserid(String userid) {this.userid = userid;}public String getUserbalace() {return userbalace;}public void setUserbalace(String userbalace) {this.userbalace = userbalace;}public String getLogintime() {return logintime;}public void setLogintime(String logintime) {this.logintime = logintime;}public String getPaymoney() {return paymoney;}public void setPaymoney(String paymoney) {this.paymoney = paymoney;}private String paymoney;private String userpwd;private String userid;private String userbalace;   //用户余额private String logintime;    //注册时间}
dao层

package com.sm.dao;import java.util.List;public interface IDaoSM<T>{/* * 添加接口 */public int addItem(T t);/* *  删除接口  */public int removeItem(Object obj);/* *  查找接口 */public T getModel(Object obj);/* * 查找接口(返回集合) */public List<T> getList(T t);}
userMapper.java

package com.sm.dao.imp;import java.util.List;import com.sm.dao.IDaoSM;import com.sm.model.userInfo;public interface userMapper extends IDaoSM<userInfo>{}
service层

package com.sm.services;import java.util.List;import org.springframework.stereotype.Controller;import com.sm.dao.imp.userMapper;import com.sm.model.userInfo;@Controllerpublic class UserInfoService {private userMapper uMapper;public userMapper getuMapper() {return uMapper;}public void setuMapper(userMapper uMapper) {this.uMapper = uMapper;}public int addItem(userInfo u){   return uMapper.addItem(u);}public List<userInfo> getList(userInfo u){return uMapper.getList(u);}/* * 注册 */public boolean register(userInfo u){if(uMapper.addItem(u)>0){return true;}else{return false;}}/* * 登录 */public userInfo login(userInfo u){List<userInfo> ulist=uMapper.getList(u);if(ulist.size()>0){return ulist.get(0);}else{return null;}}}
action层

package com.sm.action;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.sm.services.UserInfoService;import com.sm.model.userInfo;@Controllerpublic class userAction {       private UserInfoService userInfoService;public UserInfoService getUserInfoService() {return userInfoService;}public void setUserInfoService(UserInfoService userInfoService) {this.userInfoService = userInfoService;}@RequestMapping(value="/reg.action")public void reg(userInfo u){ if(userInfoService.register(u)){ System.out.println("操作成功"); }else{ System.out.println("操作失败"); }}}
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>ssm</title>  </head>    <body>  <form id="form1" action="reg.action">       昵称:<input type="text" name="username" ><br>       密码: <input type="text" name="userpwd"><br>   id:<input type="text" name="userid"><br>      <!-- id和注册时间这里偷个懒就写死了 -->        注册时间:<input type="text" name="logintime"><br>        <input type="submit">  </form>  </body></html>
下面配置 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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>SpringMybatis</display-name>  <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>     <!-- 配置前端控制器 -->  <servlet>     <servlet-name>springmvc2</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <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>springmvc2</servlet-name>     <url-pattern>*.action</url-pattern><!--配置的访问路径,一定是按照这种格式写  -->  </servlet-mapping>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>    <!-- 解决post提交乱码的问题 -->  <filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param>  </filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>  </web-app>
mybatis-config.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><!-- 编写实例别名 -->  <typeAliases>         <typeAlias alias="User" type="com.sm.model.userInfo"/>   </typeAliases>   <mappers>    <mapper resource="com/sm/model/userInfoMapper.xml"/>  </mappers></configuration>
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:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    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.xsd"        default-autowire="byName"        > <!-- 用注解的方式 --> <context:annotation-config>           <mvc:annotation-driven>           </mvc:annotation-driven> </context:annotation-config> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>  <!-- 将该包扫描到spring容器下 --> <context:component-scan base-package="com.sm.action"></context:component-scan> <context:component-scan base-package="com.sm.services"></context:component-scan> </beans>
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"      xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:p="http://www.springframework.org/schema/p"     xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd"    default-autowire="byName"    >          <!-- 配置数据源--><bean id="jdbcDataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property><property name="url"><value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value></property><property name="username"><value>p2p</value></property><property name="password"><value>123</value></property></bean>      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="jdbcDataSource"></property>        <property name="configLocation" value="classpath:mybatis-config.xml"></property>     </bean>      <!--配置dao层 -->     <bean id="uMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">       <property name="mapperInterface" value="com.sm.dao.imp.userMapper"></property>       <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>   </bean>   </beans>

撒花~








0 0
原创粉丝点击