目录
导读:如果需要源代码(前端代码)请评论或者私信,会给大家发百度网盘链接,
1.com.ljh.ssm.dao
1.1 MemberDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Member;
import org.apache.ibatis.annotations.Select;
public interface MemberDao {
@Select("select * from member where id = #{id}")
public Member findById(int id) throws Exception;
}
1.2 OrdersDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Member;
import com.ljh.ssm.domain.Orders;
import com.ljh.ssm.domain.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface OrdersDao {
@Select("select * from orders")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "orderNum",column = "orderNum"),
@Result(property = "orderTime",column = "orderTime"),
@Result(property = "orderStatus",column = "orderStatus"),
@Result(property = "peopleCount",column = "peopleCount"),
@Result(property = "payType",column = "payType"),
@Result(column = "orderDesc",property = "orderDesc"),
@Result(column = "productId",property = "product",one = @One(select =
"com.ljh.ssm.dao.ProductDao.findById"))
})
public List<Orders> findAll() throws Exception;
//涉及到多表操作
@Select("select * from orders where id = #{ordersId}")
@Results(value = {
@Result(id = true,property = "id",column = "id"),
@Result(property = "orderNum",column = "orderNum"),
@Result(property = "orderTime",column = "orderTime"),
@Result(property = "orderStatus",column = "orderStatus"),
@Result(property = "peopleCount",column = "peopleCount"),
@Result(property = "payType",column = "payType"),
@Result(column = "orderDesc",property = "orderDesc"),
@Result(column = "productId",property = "product",javaType = Product.class,one = @One(select =
"com.ljh.ssm.dao.ProductDao.findById")),
@Result(property = "member",column = "memberId",javaType = Member.class,one = @One(select =
"com.ljh.ssm.dao.MemberDao.findById")),
@Result(property = "travellers",column = "id",javaType = java.util.List.class,many = @Many(select =
"com.ljh.ssm.dao.TravellerDao.findBuOrdersId"))
})
public Orders findById(int ordersId) throws Exception;
}
1.3 PermissionDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Permission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface PermissionDao {
@Select("select * from permission where id in (select permissionId from role_permission where roleId = #{id})")
public List<Permission> findPermissionByRoleId(int id);
@Select("select * from permission")
List<Permission> findAll();
@Insert("insert into permission(permissionName,url) value(#{permissionName},#{url})")
void save(Permission permission);
}
1.4 ProductDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Product;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface ProductDao {
/**
* 查询所有的产品信息
* @return
*/
@Select("select * from product")
public List<Product> findAll();
/**
* 添加
* @param product
*/
@Insert("insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
void save(Product product);
@Select("select * from product where id = #{id}")
public Product findById(int id) throws Exception;
}
1.5 RoleDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.domain.Product;
import com.ljh.ssm.domain.Role;
import org.apache.ibatis.annotations.*;
import javax.tools.JavaFileManager;
import java.util.List;
public interface RoleDao {
@Select("select * from role where id in(select roleId from users_role where userId = #{userId})")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "roleName",column = "roleName"),
@Result(property = "roleDesc",column = "roleDesc"),
@Result(property = "permissions",column = "id",javaType = java.util.List.class ,many = @Many(select = "com.ljh.ssm.dao.PermissionDao.findPermissionByRoleId"))
})
public List<Role> findRoleByUserId(int userId) throws Exception;
@Select("select * from role")
List<Role> findAll();
@Insert("insert into role (roleName,roleDesc) value(#{roleName},#{roleDesc})")
void save(Role role);
@Select("select * from role where id = #{id}")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "roleName",column = "roleName"),
@Result(property = "roleDesc",column = "roleDesc"),
@Result(property = "permissions",column = "id",javaType = java.util.List.class,many = @Many(select = "com.ljh.ssm.dao.PermissionDao.findPermissionByRoleId"))
})
Role findById(int id);
@Delete("delete from users_role where roleId = #{id}")
void deleteFromUser_RoleByRoleId(int id);
@Delete("delete from role_permission where roleId = #{id}")
void deleteFromRole_PermissionByRoleId(int id);
@Delete("delete from role where id = #{id}")
void deleteRoleById(int id);
@Select("select * from permission where id not in (select permissionId from role_permission where roleId = #{roleId})")
List<Permission> findOtherPermissions(int roleId);
@Insert("insert into role_permission (roleId,permissionId) values (#{roleId},#{permissionId})")
void addPermissionToRole(@Param("roleId") int roleId, @Param("permissionId") int permissionId);
}
1.6 SysLogDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.SysLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface SysLogDao {
@Insert("insert into syslog (visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
public void save(SysLog sysLog);
@Select("select * from syslog")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="visitTime",property="visitTime"),
@Result(column="ip",property="ip"),
@Result(column="url",property="url"),
@Result(column="executionTime",property="executionTime"),
@Result(column="method",property="method"),
@Result(column="username",property="username")
})
List<SysLog> findAll();
}
1.7 TravellrDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Traveller;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TravellerDao {
@Select("select * from traveller where id in (select travellerId from order_traveller where orderId = #{ordersId})")
public List<Traveller> findBuOrdersId(int ordersId);
}
1.8 UserDao
package com.ljh.ssm.dao;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDao {
@Select("select * from users where username=#{username}")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "password", column = "password"),
@Result(property = "phoneNum", column = "phoneNum"),
@Result(property = "status", column = "status"),
@Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.ljh.ssm.dao.RoleDao.findRoleByUserId"))
})
public UserInfo findByUsername(String username) throws Exception;
@Select("select * from users")
List<UserInfo> findAll();
@Insert("insert into users (email,username,password,phoneNum,status) values (#{email},#{username},#{password},#{phoneNum},#{status})")
void save(UserInfo userInfo);
@Select("select * from users where id = #{id}")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "password", column = "password"),
@Result(property = "phoneNum", column = "phoneNum"),
@Result(property = "status", column = "status"),
@Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.ljh.ssm.dao.RoleDao.findRoleByUserId"))
})
UserInfo findById(int id);
@Select("select * from role where id not in (select roleId from users_role where userId = #{userId})")
List<Role> findOtherRoles(int userId);
@Insert("insert into users_role (userId,roleId) values (#{userId},#{roleId})")
void addRoleToUser(@Param("userId") int userId, @Param("roleId") int roleId);
}
2.com.ljh.ssm.domain
2.1 Member
package com.ljh.ssm.domain;
public class Member {
private int id;
private String name;
private String nickname;
private String phoneNum;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
", phoneNum='" + phoneNum + '\'' +
", email='" + email + '\'' +
'}';
}
}
2.2 Orders
package com.ljh.ssm.domain;
import com.ljh.ssm.utils.DateUtils;
import java.util.Date;
import java.util.List;
public class Orders {
private int id;
private String orderNum;
private Date orderTime;
private String orderTimeStr;
private int orderStatus;
private String orderStatusStr;
private int peopleCount;
private Product product;
private List<Traveller> travellers;
private Member member;
private Integer payType;
private String payTypeStr;
private String orderDesc;
public int getId() {
return id;
}
public String getOrderStatusStr() {
//订单状态
if (orderStatus == 0) {
orderStatusStr = "未支付";
} else if (orderStatus == 1) {
orderStatusStr = "已支付";
}
return orderStatusStr;
}
public void setOrderStatusStr(String orderStatusStr) {
this.orderStatusStr = orderStatusStr;
}
public void setId(int id) {
this.id = id;
}
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public String getOrderTimeStr() {
if (orderTime != null) {
orderTimeStr = DateUtils.data2String(orderTime, "yyyy-MM-dd HH:mm");
}
return orderTimeStr;
}
public void setOrderTimeStr(String orderTimeStr) {
this.orderTimeStr = orderTimeStr;
}
public int getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(int orderStatus) {
this.orderStatus = orderStatus;
}
public int getPeopleCount() {
return peopleCount;
}
public void setPeopleCount(int peopleCount) {
this.peopleCount = peopleCount;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public List<Traveller> getTravellers() {
return travellers;
}
public void setTravellers(List<Traveller> travellers) {
this.travellers = travellers;
}
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public Integer getPayType() {
return payType;
}
public void setPayType(Integer payType) {
this.payType = payType;
}
public String getPayTypeStr() {
if(payType==0){
payTypeStr="支付宝";
}else if(payType==1){
payTypeStr="微信支付";
}else if(payType==2){
payTypeStr="其他";
}
return payTypeStr;
}
public void setPayTypeStr(String payTypeStr) {
this.payTypeStr = payTypeStr;
}
public String getOrderDesc() {
return orderDesc;
}
public void setOrderDesc(String orderDesc) {
this.orderDesc = orderDesc;
}
@Override
public String toString() {
return "Orders{" +
"id=" + id +
", orderNum='" + orderNum + '\'' +
", orderTime=" + orderTime +
", orderTimeStr='" + orderTimeStr + '\'' +
", orderStatus=" + orderStatus +
", orderStatusStr=" + orderStatusStr +
", peopleCount=" + peopleCount +
", product=" + product +
", travellers=" + travellers +
", member=" + member +
", payType=" + payType +
", payTypeStr='" + payTypeStr + '\'' +
", orderDesc='" + orderDesc + '\'' +
'}';
}
}
2.3 Permission
package com.ljh.ssm.domain;
import java.util.List;
public class Permission {
private int id;
private String permissionName;
private String url;
private List<Role> roles;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPermissionName() {
return permissionName;
}
public void setPermissionName(String permissionName) {
this.permissionName = permissionName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "Permission{" +
"id=" + id +
", permissionName='" + permissionName + '\'' +
", url='" + url + '\'' +
", roles=" + roles +
'}';
}
}
2.4 Product
package com.ljh.ssm.domain;
import com.ljh.ssm.utils.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* 产品信息
*/
public class Product {
private String id; // 主键
private String productNum; // 编号 唯一
private String productName; // 名称
private String cityName; // 出发城市
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private Date departureTime; // 出发时间
private String departureTimeStr;
private double productPrice; // 产品价格
private String productDesc; // 产品描述
private Integer productStatus; // 状态 0 关闭 1 开启
private String productStatusStr;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductNum() {
return productNum;
}
public void setProductNum(String productNum) {
this.productNum = productNum;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Date getDepartureTime() {
return departureTime;
}
public void setDepartureTime(Date departureTime) {
this.departureTime = departureTime;
}
public String getDepartureTimeStr() {
if(departureTime!=null){
departureTimeStr = DateUtils.data2String(departureTime, "yyyy-MM-dd HH:mm:ss");
}
return departureTimeStr;
}
public void setDepartureTimeStr(String departureTimeStr) {
this.departureTimeStr = departureTimeStr;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public Integer getProductStatus() {
return productStatus;
}
public void setProductStatus(Integer productStatus) {
this.productStatus = productStatus;
}
public String getProductStatusStr() {
if (productStatus != null) {
//0关闭1开启
if (productStatus == 0) {
productStatusStr = "关闭";
}
if (productStatus == 1) {
productStatusStr = "开启";
}
}
return productStatusStr;
}
public void setProductStatusStr(String productStatusStr) {
this.productStatusStr = productStatusStr;
}
@Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", productNum='" + productNum + '\'' +
", productName='" + productName + '\'' +
", cityName='" + cityName + '\'' +
", departureTime=" + departureTime +
", departureTimeStr='" + departureTimeStr + '\'' +
", productPrice=" + productPrice +
", productDesc='" + productDesc + '\'' +
", productStatus=" + productStatus +
", productStatusStr='" + productStatusStr + '\'' +
'}';
}
}
2.5 Role
package com.ljh.ssm.domain;
import java.util.List;
public class Role {
private int id;
private String roleName;
private String roleDesc;
private List<Permission> permissions;
private List<UserInfo> users;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
public List<Permission> getPermissions() {
return permissions;
}
public void setPermissions(List<Permission> permissions) {
this.permissions = permissions;
}
public List<UserInfo> getUsers() {
return users;
}
public void setUsers(List<UserInfo> users) {
this.users = users;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
", permissions=" + permissions +
", users=" + users +
'}';
}
}
2.6 SysLog
package com.ljh.ssm.domain;
import java.util.Date;
public class SysLog {
private String id;
private Date visitTime;
private String visitTimeStr;
private String username;
private String ip;
private String url;
private Long executionTime;
private String method;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getVisitTime() {
return visitTime;
}
public void setVisitTime(Date visitTime) {
this.visitTime = visitTime;
}
public String getVisitTimeStr() {
return visitTimeStr;
}
public void setVisitTimeStr(String visitTimeStr) {
this.visitTimeStr = visitTimeStr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Long getExecutionTime() {
return executionTime;
}
public void setExecutionTime(Long executionTime) {
this.executionTime = executionTime;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
@Override
public String toString() {
return "SysLog{" +
"id='" + id + '\'' +
", visitTime=" + visitTime +
", visitTimeStr='" + visitTimeStr + '\'' +
", username='" + username + '\'' +
", ip='" + ip + '\'' +
", url='" + url + '\'' +
", executionTime=" + executionTime +
", method='" + method + '\'' +
'}';
}
}
2.7 Traveller
package com.ljh.ssm.domain;
import javax.swing.*;
import java.util.Date;
import java.util.List;
//旅客
public class Traveller {
private int id;
private String name;
private String sex;
private String phoneNum;
private Integer credentialsType;//证件类型:0身份证 1护照 2军官证
private String credentialsTypeStr;
private String credentialsNum;
private Integer travellerType;//游客类型 0成人 1儿童
private String travellerTypeStr;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public Integer getCredentialsType() {
return credentialsType;
}
public void setCredentialsType(Integer credentialsType) {
this.credentialsType = credentialsType;
}
public String getCredentialsTypeStr() {
if(credentialsType!=null){
if(credentialsType==0)
credentialsTypeStr="身份证";
}else if(credentialsType==1){
credentialsTypeStr="护照";
}else if(credentialsType==2){
credentialsTypeStr="军官证";
}
return credentialsTypeStr;
}
public void setCredentialsTypeStr(String credentialsTypeStr) {
this.credentialsTypeStr = credentialsTypeStr;
}
public String getCredentialsNum() {
return credentialsNum;
}
public void setCredentialsNum(String credentialsNum) {
this.credentialsNum = credentialsNum;
}
public Integer getTravellerType() {
return travellerType;
}
public void setTravellerType(Integer travellerType) {
this.travellerType = travellerType;
}
public String getTravellerTypeStr() {
if(travellerType!=null){
if(travellerType==0){
travellerTypeStr="成人";
}else if(travellerType==1){
travellerTypeStr="儿童";
}
}
return travellerTypeStr;
}
public void setTravellerTypeStr(String travellerTypeStr) {
this.travellerTypeStr = travellerTypeStr;
}
@Override
public String toString() {
return "Traveller{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", phoneNum='" + phoneNum + '\'' +
", credentialsType=" + credentialsType +
", credentialsTypeStr='" + credentialsTypeStr + '\'' +
", credentialsNum='" + credentialsNum + '\'' +
", travellerType=" + travellerType +
", travellerTypeStr='" + travellerTypeStr + '\'' +
'}';
}
}
2.8 UserInfo
package com.ljh.ssm.domain;
import java.io.Serializable;
import java.util.List;
/**
* 与数据库的users对应
*
*/
public class UserInfo implements Serializable {
private int id;
private String username;
private String email;
private String password;
private String phoneNum;
private int status;
private String statusStr;
private List<Role> roles;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getStatusStr() {
if (status==0){
statusStr="未开启";
}else if(status==1){
statusStr="开启";
}
return statusStr;
}
public void setStatusStr(String statusStr) {
this.statusStr = statusStr;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
", phoneNum='" + phoneNum + '\'' +
", status=" + status +
", statusStr='" + statusStr + '\'' +
", roles=" + roles +
'}';
}
}
3. com.ljh.ssm.service
3.1 接口
3.1.1 OrdersService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.Orders;
import java.util.List;
public interface OrderService {
List<Orders> findAll(int page,int size) throws Exception;
Orders findById(int ordersId) throws Exception;
}
3.1.2 PermissionService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.Permission;
import java.util.List;
public interface PermissionService {
public List<Permission> findAll();
void save(Permission permission);
}
3.1.3 ProductService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.Product;
import java.util.List;
public interface ProductService {
/**
*
* @return
* @throws Exception
*/
public List<Product> findAll(int page,int size) throws Exception;
/**
* 添加
* @param product
*/
void save(Product product);
}
3.1.4 RoleService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.domain.Role;
import java.util.List;
public interface RoleService {
public List<Role> findAll();
void save(Role role);
Role findById(int id);
void deleteRoleId(int id);
List<Permission> findOtherPermissions(int roleId);
void addPermissionToRole(int roleId, int[] permissionIds);
}
3.1.5 SysLogService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.SysLog;
import java.util.List;
public interface SysLogService {
public void save(SysLog sysLog);
List<SysLog> findAll();
}
3.1.6 UserService
package com.ljh.ssm.service;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.domain.UserInfo;
import org.springframework.security.core.userdetails.UserDetailsService;
import java.util.List;
public interface UserService extends UserDetailsService {
List<UserInfo> findAll();
void save(UserInfo userInfo);
UserInfo findById(int id);
List<Role> findOtherRoles(int userId);
void addRoleToUser(int userId, int[] roleIds);
}
3.2.1 OrderServiceImpl
package com.ljh.ssm.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import com.ljh.ssm.dao.OrdersDao;
import com.ljh.ssm.domain.Orders;
import com.ljh.ssm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class OrdersServiceImpl implements OrderService {
@Autowired
private OrdersDao ordersDao;
@Override
public List<Orders> findAll(int page,int size) throws Exception {
//pagehelper的参数 pageNum是页码值,pageSize是每页显示的条数 必须写在查询代码之前
PageHelper.startPage(page, size);
return ordersDao.findAll();
}
@Override
public Orders findById(int ordersId) throws Exception {
return ordersDao.findById(ordersId);
}
}
3.2.2 PermissionServciceImpl
package com.ljh.ssm.service.impl;
import com.ljh.ssm.dao.PermissionDao;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.service.PermissionService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PermissionServiceImpl implements PermissionService {
@Autowired
private PermissionDao permissionDao;
@Override
public List<Permission> findAll() {
return permissionDao.findAll();
}
@Override
public void save(Permission permission) {
permissionDao.save(permission);
}
}
3.2.3 ProductServiceImpl
package com.ljh.ssm.service.impl;
import com.github.pagehelper.PageHelper;
import com.ljh.ssm.dao.ProductDao;
import com.ljh.ssm.domain.Product;
import com.ljh.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public List<Product> findAll(int page,int size) throws Exception {
PageHelper.startPage(page, size);
return productDao.findAll();
}
@Override
public void save(Product product) {
productDao.save(product);
}
}
3.2.4 RoleServiceImpl
package com.ljh.ssm.service.impl;
import com.ljh.ssm.dao.RoleDao;
import com.ljh.ssm.dao.UserDao;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
RoleDao roleDao;
@Override
public List<Role> findAll() {
return roleDao.findAll();
}
@Override
public void save(Role role) {
roleDao.save(role);
}
@Override
public Role findById(int id) {
return roleDao.findById(id);
}
@Override
public void deleteRoleId(int id) {
roleDao.deleteFromUser_RoleByRoleId(id);
roleDao.deleteFromRole_PermissionByRoleId(id);
roleDao.deleteRoleById(id);
}
@Override
public List<Permission> findOtherPermissions(int roleId) {
return roleDao.findOtherPermissions(roleId);
}
@Override
public void addPermissionToRole(int roleId, int[] permissionIds) {
for (int permissionId : permissionIds) {
roleDao.addPermissionToRole(roleId,permissionId);
}
}
}
3.2.5 SysLogServiceImpl
package com.ljh.ssm.service.impl;
import com.ljh.ssm.dao.SysLogDao;
import com.ljh.ssm.domain.SysLog;
import com.ljh.ssm.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public class SysLogServiceImpl implements SysLogService {
@Autowired
private SysLogDao sysLogDao;
@Override
public void save(SysLog sysLog) {
sysLogDao.save(sysLog);
}
@Override
public List<SysLog> findAll() {
return sysLogDao.findAll();
}
}
3.2.6 UserServiceImpl
package com.ljh.ssm.service.impl;
import com.ljh.ssm.dao.UserDao;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.domain.UserInfo;
import com.ljh.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo = null;
try {
userInfo = userDao.findByUsername(username);
} catch (Exception e) {
e.printStackTrace();
}
//User user = new User(userInfo.getUsername(), "{noop}"+userInfo.getPassword(), getAuthority(userInfo.getRoles()));
User user = new User(userInfo.getUsername(),userInfo.getPassword(),userInfo.getStatus()==0?false:true,true,true,true,getAuthority(userInfo.getRoles()));
System.out.println(userInfo);
return user;
}
//作用就是返回一个list集合,集合中装入的是角色的描述
private List<SimpleGrantedAuthority> getAuthority(List<Role> roles) {
List<SimpleGrantedAuthority> list = new ArrayList<>();
for (Role role : roles) {
list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
}
return list;
}
@Override
public List<UserInfo> findAll() {
return userDao.findAll();
}
@Override
public void save(UserInfo userInfo) {
//对密码加密
userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
userDao.save(userInfo);
}
@Override
public UserInfo findById(int id) {
return userDao.findById(id);
}
@Override
public List<Role> findOtherRoles(int userId) {
return userDao.findOtherRoles(userId);
}
@Override
public void addRoleToUser(int userId, int[] roleIds) {
for (int roleId : roleIds) {
userDao.addRoleToUser(userId,roleId);
}
}
}
4.com.ljh.utils
4.1BCryptPasswordEncoderUtils
package com.ljh.ssm.utils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class BCryptPasswordEncoderUtils {
private static BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
public String encodePassword(String password){
return bCryptPasswordEncoder.encode(password);
}
public static void main(String[] args) {
String a = "123456";
String encode = bCryptPasswordEncoder.encode(a);
System.out.println(encode);
}
}
4.2 DateStringEditor
package com.ljh.ssm.utils;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import java.text.ParseException;
import java.util.Date;
/**
* 日期与字符类转换
*/
public class DateStringEditor extends PropertiesEditor {
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
Date date = DateUtils.string2Date(text, "yyyy-MM-dd HH:mm");
super.setValue(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
4.3 Dateutils
package com.ljh.ssm.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
//日期转换成字符串
public static String data2String(Date date,String patt){
SimpleDateFormat sdf = new SimpleDateFormat(patt);
String format = sdf.format(date);
return format;
}
//字符串转换字符串
public static Date string2Date(String date,String patt) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(patt);
Date parse = sdf.parse(patt);
return parse;
}
}
5. com.ljh.ssm.controller
5.1 LogAop
package com.ljh.ssm.controller;
import com.ljh.ssm.domain.SysLog;
import com.ljh.ssm.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
@Component
@Aspect
public class LogAop {
@Autowired
private HttpServletRequest request;
@Autowired
private SysLogService sysLogService;
private Date visitTime; //开始时间
private Class clazz; //访问的类
private Method method;//访问的方法
//前置通知 主要是获取开始时间,执行的类是哪一个,执行的是哪一个方法
@Before("execution(* com.ljh.ssm.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws NoSuchMethodException {
visitTime = new Date();//当前时间就是开始访问的时间
clazz = jp.getTarget().getClass(); //具体要访问的类
String methodName = jp.getSignature().getName(); //获取访问的方法的名称
Object[] args = jp.getArgs();//获取访问的方法的参数
//获取具体执行的方法的Method对象
if (args == null || args.length == 0) {
method = clazz.getMethod(methodName); //只能获取无参数的方法
} else {
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
clazz.getMethod(methodName,classArgs);
}
}
//后置通知
@After("execution(* com.ljh.ssm.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
long time = new Date().getTime() - visitTime.getTime(); //获取访问的时长
String url = "";
//获取url
if (clazz != null && method != null && clazz != LogAop.class) {
//1.获取类上的@RequestMapping("/orders")
RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if (classAnnotation != null) {
String[] classValue = classAnnotation.value();
//2.获取方法上的@RequestMapping(xxx)
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String[] methodValue = methodAnnotation.value();
url = classValue[0] + methodValue[0];
//获取访问的ip
String ip = request.getRemoteAddr();
//获取当前操作的用户
SecurityContext context = SecurityContextHolder.getContext();//从上下文中获了当前登录的用户
User user = (User) context.getAuthentication().getPrincipal();
String username = user.getUsername();
//将日志相关信息封装到SysLog对象
SysLog sysLog = new SysLog();
sysLog.setExecutionTime(time); //执行时长
sysLog.setIp(ip);
sysLog.setMethod("[类名]: " + clazz.getName() + " [方法名]:" + method.getName());
sysLog.setUrl(url);
sysLog.setUsername(username);
sysLog.setVisitTime(visitTime);
System.out.println(sysLog);
//调用Service完成操作
sysLogService.save(sysLog);
}
}
}
}
}
5.2 OrdersController
package com.ljh.ssm.controller;
import com.github.pagehelper.PageInfo;
import com.ljh.ssm.dao.OrdersDao;
import com.ljh.ssm.domain.Orders;
import com.ljh.ssm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/orders")
public class OrdersController {
@Autowired
private OrderService orderService;
// //查询全部订单,没有分页
// @RequestMapping("/findAll.do")
// public ModelAndView findAll() throws Exception {
// ModelAndView mv = new ModelAndView();
// List<Orders> all = orderService.findAll();
// mv.addObject("ordersList",all);
// mv.setViewName("orders-list");
// return mv;
// }
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") Integer page,@RequestParam(name = "size",required = true,defaultValue = "4") Integer size) throws Exception{
ModelAndView mv = new ModelAndView();
List<Orders> ordersList = orderService.findAll(page, size);
//pageInfo就是一个分页的bean对象
PageInfo pageInfo = new PageInfo(ordersList);
mv.addObject("PageInfo",pageInfo);
mv.setViewName("orders-page-list");
return mv;
}
@RequestMapping("findById.do")
public ModelAndView findById(@RequestParam(name = "id",required = true)Integer ordersId) throws Exception {
ModelAndView mv = new ModelAndView();
Orders orders = orderService.findById(ordersId);
mv.addObject("orders",orders);
mv.setViewName("orders-show");
return mv;
}
}
5.3 PermissionController
package com.ljh.ssm.controller;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.service.PermissionService;
import org.apache.ibatis.annotations.Result;
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;
import java.util.List;
@Controller
@RequestMapping("/permission")
public class PermissionController {
@Autowired
private PermissionService permissionService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
List<Permission> all = permissionService.findAll();
mv.addObject("permissionList",all);
mv.setViewName("permission-list");
return mv;
}
@RequestMapping("/save.do")
public String save(Permission permission){
permissionService.save(permission);
return "redirect:findAll.do";
}
}
5.4 ProductController
package com.ljh.ssm.controller;
import com.github.pagehelper.PageInfo;
import com.ljh.ssm.domain.Product;
import com.ljh.ssm.service.ProductService;
import com.ljh.ssm.utils.DateStringEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.security.RolesAllowed;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
/**
* 类型转换
*/
// @InitBinder
// public void initBinder(WebDataBinder binder){
// binder.registerCustomEditor(Date.class, new DateStringEditor());
// }
/**
* 查询全部产品没有分页
* @return
* @throws Exception
*/
// @RequestMapping("/findAll.do")
// public ModelAndView findAll() throws Exception {
// ModelAndView mv = new ModelAndView();
// List<Product> ps = productService.findAll();
// mv.addObject("productList",ps);
// mv.setViewName("product-list");
// return mv;
// }
/**
* 查找
* @param page
* @param size
* @return
* @throws Exception
*/
@RequestMapping("findAll.do")
// @RolesAllowed("ADMIN")
@Secured("ROLE_ADMIN")
public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1") Integer page,@RequestParam(name = "size",required = true,defaultValue = "4") Integer size) throws Exception {
ModelAndView mv = new ModelAndView();
List<Product> productList = productService.findAll(page, size);
PageInfo pageInfo = new PageInfo(productList);
mv.addObject("pageInfo",pageInfo);
mv.setViewName("product-page-list");
return mv;
}
/**
* 产品添加
* @param product
*/
@RequestMapping("/save.do")
public String save(Product product) throws Exception {
productService.save(product);
return "redirect:findAll.do";
}
}
5.5 RoleController
package com.ljh.ssm.controller;
import com.ljh.ssm.domain.Permission;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.service.RoleService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleService roleService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView mv =new ModelAndView();
List<Role> roleList = roleService.findAll();
mv.addObject("roleList",roleList);
mv.setViewName("role-list");
return mv;
}
@RequestMapping("/save.do")
public String save(Role role){
roleService.save(role);
return "redirect:findAll.do";
}
@RequestMapping("/findById.do")
public ModelAndView findById(int id){
ModelAndView mv = new ModelAndView();
Role role = roleService.findById(id);
mv.addObject("role",role);
mv.setViewName("role-show");
return mv;
}
@RequestMapping("/deleteRole.do")
public String deleteRole(int id){
roleService.deleteRoleId(id);
return "redirect:findAll.do";
}
//根据roleId传旭role,并且查询出可以添加的权限
@RequestMapping("findRoleByIdAndAllPermission.do")
public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = "id",required = true) Integer roleId){
ModelAndView mv = new ModelAndView();
//根据roleId查询role
Role byId = roleService.findById(roleId);
//根据roleId查询可以添加的权限
List<Permission> otherPermission = roleService.findOtherPermissions(roleId);
mv.addObject("role",byId);
mv.addObject("permissionList",otherPermission);
mv.setViewName("role-permission-add");
return mv;
}
//给角色添加权限
@RequestMapping("/addPermissionToRole.do")
public String addPermissionToRole(@RequestParam(name = "roleId",required = true)Integer roleId,@RequestParam(name = "ids",required = true)int[] permissionIds){
roleService.addPermissionToRole(roleId,permissionIds);
return "redirect:findAll.do";
}
}
5.6 SysLogController
package com.ljh.ssm.controller;
import com.ljh.ssm.domain.SysLog;
import com.ljh.ssm.service.SysLogService;
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;
import java.util.List;
@Controller
@RequestMapping("/sysLog")
public class SysLogController {
@Autowired
private SysLogService sysLogService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
List<SysLog> sysLogList = sysLogService.findAll();
mv.addObject("sysLogs",sysLogList);
mv.setViewName("syslog-list");
return mv;
}
}
5.7 UserController
package com.ljh.ssm.controller;
import com.ljh.ssm.domain.Role;
import com.ljh.ssm.domain.UserInfo;
import com.ljh.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.nio.channels.Pipe;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
List<UserInfo> userInfoList = userService.findAll();
mv.addObject("userList",userInfoList);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("/save.do")
public String save(UserInfo userInfo){
userService.save(userInfo);
return "redirect:findAll.do";
}
@RequestMapping("/findById.do")
public ModelAndView findById(Integer id){
ModelAndView mv = new ModelAndView();
UserInfo userInfo =userService.findById(id);
mv.addObject("user",userInfo);
mv.setViewName("user-show1");
return mv;
}
/**
* 查询用户以及用户可以添加的角色
* @return
*/
@RequestMapping("/findUserByIdAndAllRole.do")
public ModelAndView findUserByIdAndAllRole(@RequestParam(name = "id",required = true) Integer userId){
ModelAndView mv = new ModelAndView();
//1.根据用户id查询用户
UserInfo userInfo = userService.findById(userId);
//2.根据用户id查询可以添加的角色
List<Role> otherRoles = userService.findOtherRoles(userId);
mv.addObject("user",userInfo);
mv.addObject("roleList",otherRoles);
mv.setViewName("user-role-add");
return mv;
}
//给用户添加角色
@RequestMapping("addRoleToUser.do")
public String addRoleToUser(@RequestParam(name = "userId",required = true)Integer userId,@RequestParam(name = "ids",required = true)int[] roleIds){
userService.addRoleToUser(userId,roleIds);
return "redirect:findAll.do";
}
}