写点什么

SSM 框架示例(适合新手)(1)

  • 2021 年 11 月 11 日
  • 本文字数:5298 字

    阅读完需:约 17 分钟

public void setUname(String uname) {


this.uname = uname;


}


public int getUpass() {


return upass;


}


public void setUpass(int upass) {


this.upass = upass;


}


public User(int uid, String uname, int upass) {


super();


this.uid = uid;


this.uname = uname;


this.upass = upass;


}


public User() {


// TODO Auto-generated constructor stub


}


}


2.UserDao? 配置接口???包名:lx.dao(根据自己的习惯定义就可以)


package lx.dao;


import java.util.List;


import lx.entity.User;


public interface UserDao {


public List<User> findAll();


public int save(User u);


public int delete(int id);


public int update(User u);


public User findById(int id);


}


(2)配置? My Batis 配置文件?UserMapper.xml


<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE mapper PUBLIC "-//mybatis.org/


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


/DTD Mapper 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="lx.dao.UserDao">


<select id="findAll" resultType="User">


select * from user


</select>


<insert id="save" parameterType="User">


insert into user(uname,upass) values (#{uname},#{upass})


</insert>


<delete id="delete" parameterType="int">


delete from user where uid=#{uid}


</delete>


<update id="update" parameterType="User">


update user set uname=#{uname},upass=#{upass} where uid=#{uid}


</update>


<select id="findById" parameterType="int" resultType="User">


select * from user where uid=#{uid}


</select>


</mapper>


3.UserDaoImpl? 配置实现类??包名:lx.dao.impl(根据自己的习惯定义就可以)


SSM 采用注解的方式进行扫描,不需要写复杂的步骤,只需要用对象获取他的方法


package lx.dao.impl;


import java.util.List;


import lx.dao.UserDao;


import lx.entity.User;


import org.springframework.beans.factory.annotation.Autowired;


public class UserDaoImpl implements UserDao{


@Autowired


private UserDao user;


public List<User> findAll() {


// TODO Auto-generated method stub


return user.findAll();


}


public int save(User u) {


// TODO Auto-generated method stub


return user.save(u);


}


public int delete(int id) {


// TODO Auto-generated method stub


return user.delete(id);


}


public int update(User u) {


// TODO Auto-generated method stub


return user.update(u);


}


public User findById(int id) {


// TODO Auto-generated method stub


return user.findById(id);


}


}


4.UserSerivce?配置我们的业务逻辑层??包名:lx.service(根据自己的习惯定义就可以)


service 层的接口与 dao 层一样


package lx.service;


import java.util.List;


import lx.entity.User;


public interface UserSerivce {


public List<User> findAll();


public boolean save(User u);


public boolean delete(int id);


public boolean update(User u);


public User findById(int id);


}


5.UserServiceImpl??包名:lx.service.Impl(根据自己的习惯定义就可以)


package lx.service.impl;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import lx.dao.UserDao;


import lx.entity.User;


import lx.service.UserSerivce;


@Service


public class UserServiceImpl implements UserSerivce{


@Autowired


private UserDao usi;


public List<User> findAll() {


// TODO Auto-generated method stub


return usi.findAll();


}


public boolean save(User u) {


// TODO Auto-generated method stub


int a=usi.save(u);


if(a==1){


return true;


}else{


return false;


}


}


public boolean delete(int id) {


// TODO Auto-generated method stub


int a=usi.delete(id);


if(a==1){


return true;


}else{


return false;


}


}


public boolean update(User u) {


// TODO Auto-generated method stub


int a=usi.update(u);


if(a==1){


return true;


}else{


return false;


}


}


public User findById(int id) {


// TODO Auto-generated method stub


return usi.findById(id);


}


}


6.UserAction? ?包名:lx.action(根据自己的习惯定义就可以)


package lx.action;


import lx.entity.User;


import lx.service.UserSerivce;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.servlet.ModelAndView;


@Controller


public class UserAction {


@Autowired


private UserSerivce user;


@RequestMapping(value="showuser")


public ModelAndView find(){


//先 new 一个 ModelAndView 对象


ModelAndView mav=new ModelAndView();


//以键值队的形式赋值 然后用对象调用方法


mav.addObject("user",user.findAll());


//要跳转的页面


mav.setViewName("list_user");


//return 出来你 new 的对象


return mav;


}


//增加


@RequestMapping(value="saveuser")


public ModelAndView save(User u){


user.save(u);


return new ModelAndView("redirect:/showuser.do");


}


//删除


@RequestMapping(value="deleteuser")


public ModelAndView delete(int id){


user.delete(id);


return new ModelAndView("redirect:/showuser.do");


}


//预修改


@RequestMapping(value="preupdateuser")


public ModelAndView preupdate(int id){


ModelAndView mav=new ModelAndView();


mav.addObject("user", user.findById(id));


mav.setViewName("update_user");


return mav;


}


//修改


@RequestMapping(value="updateuser")


public ModelAndView update(User u){


user.update(u);


return new ModelAndView("redirect:/showuser.do");


}


}


7.配置 MyBatis 的配置文件??MyBatisConf.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="lx.entity.User"/>


</typeAliases>


<mappers>


<mapper resource="lx/dao/UserMapper.xml"/>


</mappers>


</configuration>


(2)Spring MVC 的配置文件


SpringConf.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:aop="http://www.springframework.org/schema/aop"


xsi:schemaLocation="


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.2.xsd


http://www.springframework.org/schema/aop


http://www.springframework.org/schema/aop/spring-aop-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >


<property name="location">


<value>classpath:jdbc.properties</value>


</property>


</bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">


<property name="driverClassName" value="${jdbc.driver}"></property>


<property name="url" value="${jdbc.url}"></property>


<property name="username" value="${jdbc.username}"></property>


<property name="password" value="${jdbc.password}"></property>


</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">


<property name="dataSource" ref="dataSource" />


<property name="configLocation" value="classpath:MyBatisConf.xml" />


<!--


它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。


多个 package 之间可以用逗号或者分号等来进行分隔。(value 的值一定要是包的全名)


-->


<property name="typeAliasesPackage" value="lx.entity" />


</bean>


<!-- 注入映射器


为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,


MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。


当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。


使用注入的映射器代码,在 MyBatis,Spring 或 MyBatis-Spring 上面不会有直接的依赖。


MapperFactoryBean 创建的代理控制开放和关闭 session,翻译任意的异常到 Spring 的 DataAccessException 异常中。


此外,如果需要或参与到一个已经存在活动事务中,代理将 会开启一个新的 Spring 事务。


-->


<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">


<property name="mapperInterface" value="lx.dao.UserDao" />


<property name="sqlSessionFactory" ref="sqlSessionFactory" />


</bean>


<context:component-scan base-package="lx.*" />


</beans>


SpringMVC-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"


xmlns:context="http://www.springframework.org/schema/context"


xmlns:util="http://www.springframework.org/schema/util" 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"


xsi:schemaLocation="


http://www.springframework.org/schema/util


http://www.springframework.org/schema/util/spring-util-3.2.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.2.xsd">


mvc:annotation-driven/


<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>


<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>


<context:component-scan base-package="lx.*" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">


<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>


</bean>


</beans>


JSP 页面部分:


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>My JSP 'index.jsp' starting page</title>


<meta http-equiv="pragma" content="no-cache">


<meta http-equiv="cache-control" content="no-cache">


<meta http-equiv="expires" content="0">


<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">


<meta http-equiv="description" content="This is my page">


<!--


<link rel="stylesheet" type="text/css" href="styles.css">


-->


</head>


<body>


<a href="showuser.do">查询</a>


</body>


</html>


list_user.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%


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>My JSP 'list_user.jsp' starting page</title>


<meta http-equiv="pragma" content="no-cache">


<meta http-equiv="cache-control" content="no-cache">


<meta http-equiv="expires" content="0">


<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">


<meta http-equiv="description" content="This is my page">


<!--


<link rel="stylesheet" type="text/css" href="styles.css">


-->


</head>


<body>


<table border="1">


<tr>


<td>编号</td>


<td>用户名</td>


<td>密码</td>


<td>可执行操作</td>


</tr>


<c:forEach var="user" items="${user}">


<tr>


<td>${user.uid}</td>


<td>${user.uname}</td>


<td>${user.upass}</td>


<td>

评论

发布
暂无评论
SSM框架示例(适合新手)(1)