ssm框架mysql自增_学习并搭建SSM框架有没有很容易记的方法?

记住SSM的几大关键步骤即可,多操作几次就可以随手搭建SSM框架系统。

三大重点:依赖包管理、框架配置文件、项目骨架。

除了项目骨架是需要自己记住,其他的都是直接拷贝到新项目即可使用。

关注我的知乎专栏获取更多Java知识:依赖包管理pom.xml 直接备份,搭建新项目复制过去即可。

2. 配置环境这些配置文件也是备份,搭建新项目直接复制过去即可。

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/ssm2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10

jdbc.username=root

jdbc.password=123456持久层框架配置(mybatis-config.xml):

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

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

容器配置,将spring配置分为三份,便于管理和理解:

spring-dao.xml

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.xsd

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

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

spring-service.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

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

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

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

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

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

spring-web.xml

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.xsd

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

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

web.xml配置

ssm

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:spring.xml

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

springmvc

*.action

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

CharacterEncodingFilter

/*

3.项目骨架entity中放入实体类。

package cn.ssm.entity;

public class User {

private Integer id;

private String code;

private String password;

private String name;

private String phone;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String toString() {

return "User [id=" + id + ", code=" + code + ", name=" + name + ", phone=" + phone + "]";

}

}dao中是增删改查的接口,dao中还有一个mapper文件夹,存放映射文件。

package cn.ssm.dao;

import cn.ssm.entity.User;

public interface UserDao {

void login(String code,String password);//登陆

int add(User user);//注册

int queryAllById(int id);//查询

}映射文件放在dao下面的mapper文件夹中

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

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

SELECT

code,

password

FROM

user

where

code=#{code}

and

password=#{password}

SELECT

id,

code,

name,

phone,

FROM

user

WHERE

id=#{id}

insert into

user(

code,

password,

name,

phone)

values(

code=#{code},

password=#{password},

name=#{name},

phone=#{phone})

service文件夹中是业务逻辑接口和实现类

package cn.ssm.service;

import cn.ssm.entity.User;

public interface UserService {

/**

* 登录验证

* @param account 登录账号

* @param userPassword 密码

* @return validate true通过 false未通过

* @throws Exception

*/

User login(String code, String password) throws Exception;

/**

* 注册

* @param user

*/

int add(User user)throws Exception;

/**

* ID查询

* @param id

* @return

* @throws Exception

*/

User queryById(int id)throws Exception;

}

实现类

package cn.ssm.service;

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

import cn.ssm.dao.UserDao;

import cn.ssm.entity.User;

public class UserServiceImpl implements UserService {

@Autowired

UserDao userDao;

@Override

public User login(String code, String password) throws Exception {

return userDao.login(code, password);

}

@Override

public int add(User user) throws Exception {

return userDao.add(user);

}

@Override

public User queryById(int id) throws Exception {

return userDao.queryById(id);

}

}web中放入控制器

package cn.ssm.web;

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

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

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

import cn.ssm.entity.User;

import cn.ssm.service.UserService;

@Controller

@RequestMapping("/user")

public class UserCtroller {

@Autowired

UserService userService;

@RequestMapping("login")

public String login(String code, String password, Model model) throws Exception {

System.out.println("用户登陆" + code + password);

userService.login(code, password);

model.addAttribute("sg", "登陆成功");

return "show";

}

@RequestMapping("add")

public String add(User user, Model model) throws Exception {

System.out.println("用户注册" + user.getCode() + user.getPassword());

int addUser = userService.add(user);

model.addAttribute("sg", "注册成功");

if (addUser == 1) {

return "login";

}

return null;

}

@RequestMapping("query")

public String queryById(int id, Model model) throws Exception {

System.out.println("查询id" + id);

userService.queryById(id);

model.addAttribute("sg", "查询结果");

return "show";

}

}

JSP页面

注册

pageEncoding="UTF-8"%>

用户注册
用户名
密码

登陆

pageEncoding="UTF-8"%>

用户登录
用户名
密码

登陆成功

pageEncoding="UTF-8"%>

登录成功

${msg }

去登录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值