https://github.com/DillonPu/ssm.git
#创建数据库ssm
drop database if exists ssm;
create database ssm;
use ssm;
#创建书本表
drop table if exists ssm_book;
create table ssm_book (
id int(11) primary key auto_increment not null,
name varchar(111),
detail varchar(256),
userId int(22)
);
insert into ssm_book(id, name,detail,userId) values
(1,"javaWeb","JavaWeb从入门到精通",1),
(2,"PhotoShop","图片图像处理",1),
(3,"mysql从入门到精通","数据库专业书籍",2);
select * from ssm_book;
# 创建用户表
create table ssm_user (
id int(11) primary key auto_increment not null,
username varchar(111),
password varchar(256)
);
insert into ssm_user(id, username,password) values
(1,"aa","aa"),
(2,"bb","bb"),
(3,"cc","cc");
select * from ssm_userssm_user;
select * from ssm_book where userId = 1;
package com.ssm.pojo;
public class Book {
private Integer id;
private String name;
private String detail;
private Integer userId;
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 getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", detail=" + detail + ", userId=" + userId + "]";
}
}
package com.ssm.pojo;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
我直接放在了src下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin
由于在applicationContext.xml配置了自动扫描mapper,所以需要把mapper类和其对应的mapper.xml文件放在同一目录下,命名也是一样
package com.ssm.mapper;
import java.util.List;
import com.ssm.pojo.Book;
public interface BookMapper {
//添加一本书
public void addBook(Book book);
//根据id删除一本书
public void deleteBookById(Integer id);
//根据ID查询书
public Book findBookById(Integer id);
//根据书名模糊查询书
public List findBooksByKeyname(String keyname);
//根据id编辑修改书的信息
public void editBookInformationById(Book book);
//根据用户id查询
public List findBooksByUserId(Integer userId);
}
insert into ssm_book(id, name,detail,userId) values(#{id},#{name},#{detail},#{userId})
delete from ssm_book where id = #{id}"
update ssm_book
set name=#{name},detail=#{detail},userId=#{userId}
where id=#{id}
package com.ssm.mapper;
import org.apache.ibatis.annotations.Param;
import com.ssm.pojo.User;
public interface UserMapper {
//查找用户
public User findUserbyNameAndPwd(@Param("username") String username,@Param("password") String password);
public User findUserbyName(String username);
}
package com.ssm.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
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 com.ssm.pojo.Book;
import com.ssm.pojo.User;
import com.ssm.service.BookService;
import com.ssm.service.LoginService;
@Controller
public class Login {
/**
* @author AL
* 登录
*/
@Autowired
private LoginService loginService;
@Autowired
private BookService bookService;
@RequestMapping("login")
public String login(String username,String password,Model model) {
User user = this.loginService.findUserByNameAndPwd(username,password);
if(user!=null) {
Integer userId = user.getId();
List books = bookService.findBooksByUserId(userId);
for(Book book : books) {
System.out.println(book);
}
model.addAttribute("user",user);
model.addAttribute("books",books);
return "my";
}
return "index";
}
}
用于操作书籍
package com.ssm.service;
import java.util.List;
import com.ssm.pojo.Book;
public interface BookService {
public List findBooksByUserId(Integer userId);
}
package com.ssm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.mapper.BookMapper;
import com.ssm.pojo.Book;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public List findBooksByUserId(Integer userId) {
List books = bookMapper.findBooksByUserId(userId);
return books;
}
}
package com.ssm.service;
import com.ssm.pojo.User;
public interface LoginService {
public User findUserByNameAndPwd(String username, String password);
}
package com.ssm.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.mapper.UserMapper;
import com.ssm.pojo.User;
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private UserMapper userMapper;
@Override
public User findUserByNameAndPwd(String username, String password) {
User user = userMapper.findUserbyNameAndPwd(username, password);
return user;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
index
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Insert title here
welcome:${user.username}
your book list
${vs.index }
${book.id }
${book.name }
${book.detail }