Marco's Java

Preface

After the previous stage of learning, we have been very familiar with Spring, Spring MVC, Maven, Shiro has entered a door, perhaps a single point of knowledge is not so difficult to understand, but once all things are "burned together", it feels like no clue?
So the Shiro+SMM integration project at the beginning of this section combines the top, middle and bottom chapters. We will combine all the knowledge together to make a "small project". This "project" does not mean how complex and difficult it is, it is more suitable for those who are not familiar with the integration process, so we have a selective view on Ok. No more nonsense, let's start.~

Construction of database and initial construction of Maven project

In the initial stage of project construction, the first question we need to think about is, what data can I manipulate? How can these data be partitioned and reflected in the database? Which data should be planned in which table? What is the relationship between each table and the table? What is the endowment of each table? Wait a minute, then you have to have a sketch in your mind. It's better to make a sketch on paper before you start. It's easier to have a clear idea after the project is built than to go to the battlefield empty-handed. As the old saying goes, "Know your friends and know your enemies, win a hundred battles", and programming is no exception.

So this section begins with the first form of "Dragon-lowering Eighteen Forms" of this cracking project.
Format 1: Create Maven Web Projects
Our first step is to create a Maven Web project. I'm not complicated here, so I don't split multiple projects according to MVC as before, because distribution is not involved yet. Maven's friends who are not familiar with the preparation of Web construction can refer to blog posts.
Marco's Java

Form 2: Preparing the database
We have prepared five tables here. Because of the many-to-many relationship among privileges, roles and users, in addition to the three main tables, two relational join tables are added. Of course, table names are optional, but these tables are necessary for the RBAC (Role Based Access Control) structure.

Permission permission table

Role role table

role_permission role permission table

user table

user_role user role table

You should be able to understand the relationship of these five tables more intuitively through the graph below. The left column is the relationship table, and the middle is our three main tables. The right side is an illustration of the corresponding roles and rights of users. The fields associated with tables can also be clearly reflected in the relationship table.

Third Form: Modify the pom.xml file
When I was studying Maven, I was blogging. Marco's Java At the end of this article, we provide a relatively complete pom.xml, but we know that many jar packages in Maven can rely on delivery. For example, if we import spring-jdbc, then spring-core, spring-beans and so on will also be imported. So here I slightly simplify, using Maven's transmission relationship, as much as possible. Less configuration of energy.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.marco</groupId>
	<artifactId>shiro_ssm_layui</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	<name>shiro_ssm_layui</name>
	<url>http://maven.apache.org</url>
	<!-- Declare version number -->
	<properties>
		<servlet.version>3.1.0</servlet.version>
		<jsp.version>2.3.1</jsp.version>
		<spring.version>4.3.24.RELEASE</spring.version>
		<mybatis.version>3.5.1</mybatis.version>
		<mybatis-spring.version>2.0.1</mybatis-spring.version>
		<mysql.version>5.1.47</mysql.version>
		<pagehelper.version>5.1.10</pagehelper.version>
		<druid.version>1.1.19</druid.version>
		<slf4j.version>1.7.26</slf4j.version>
		<log4j.version>1.2.17</log4j.version>
		<jackson.version>2.9.9</jackson.version>
		<jstl.version>1.1.2</jstl.version>
		<shiro.version>1.4.1</shiro.version>
	</properties>
	<!-- Dependent jar package -->
	<dependencies>
		<!-- servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis-spring.version}</version>
		</dependency>
		<!-- mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<!-- pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>${pagehelper.version}</version>
		</dependency>
		<!-- druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>
		<!-- slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<!-- log4j-api -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<!-- jackson-core -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<!--taglibs/standard -->
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<!-- shiro.version -->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>${shiro.version}</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>carrent_plus</finalName>
		<plugins>
			<!-- join tomcat Running plug-ins -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<!--Solve the problem of data scrambling in page submission -->
					<!-- <uriEncoding>UTF-8</uriEncoding> -->
					<!-- tomcat Plug-in's Request Port --> 
					<!-- <port>8080</port> -->
					<!-- Request path of project -->
					<!-- <path>/marco</path> -->
					<uriEncoding>UTF-8</uriEncoding><!--  Solve the problem of data scrambling in page submission -->
                    <port>8080</port><!-- tomcat Plug-in's Request Port -->
                    <!-- <path>/marco</path> --><!-- Request path of project -->
                    <!-- finger tomcat Of manager Access address of project     
                    http://127.0.0.1:8080/manager
                    text Must add, if not add, use maven Packing doesn't help us deploy the project to tomcat7 inside
                     -->
                    <url>http://localhost:8080/manager/text</url>
                    <!-- tomcat Login name and password -->
                    <username>admin</username>
                    <password>admin</password>
                    <!-- become involved war The name of the package -->
                    <path>/marco</path>
				</configuration>
			</plugin>


			<!-- Specifying the current project jdk Edition -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<!-- Appoint source and target Version of _____________ -->
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

In this way, the jar packages that our "small project" relies on are imported. Now, do you think Maven is still very simple to use? After all, we do not need to download one by one, one by one in the folder, the version may still have problems, that is more headache!~

Form 4: Create User Realm
The login operation we are going to do is quite different from before, because we need to use Shiro framework to implement our security policy, because we need to implement authentication and authorization functions, so User Realm needs to integrate Authorization Realm and rewrite doGetAuthorization Info () and doGetAuthentication Info () methods. We first create this class, and then revise and improve it. The idea will be clearer.

Basic MVC Model Building of Shiro Integrated SSM

Form 5: Creating Entity Classes
According to the database table above, we can see that we need to create three entity classes. First, I list all the directories created by the project. The code in the domain is very simple. I won't go into more details here.~

Form 6: Create UserMapper and modify UserMapper.xml
Because I'm using Mybatis reverse engineering automatically generated domain s, mappers, and Mapper.xml, some details of User mapper and Mapper.xml need to be modified.

package com.marco.mapper;

import com.marco.domain.User;

public interface UserMapper {
    int deleteByPrimaryKey(Integer userid);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer userid);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
    
    //Query user information based on user's username
    User queryUserByUserName(String username);
}

Because other additions, deletions and modifications in reverse engineering have been generated, my Mapper.xml code will only add some sql corresponding to the functions we need.

<mapper namespace="com.marco.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.marco.domain.User">
    <id column="userid" jdbcType="INTEGER" property="userid" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="userpwd" jdbcType="VARCHAR" property="userpwd" />
    <result column="sex" jdbcType="VARCHAR" property="sex" />
    <result column="address" jdbcType="VARCHAR" property="address" />
  </resultMap>
  <sql id="Base_Column_List">
    userid, username, userpwd, sex, address
  </sql>
  <select id="queryUserByUserName" resultMap="BaseResultMap">
  	select <include refid="Base_Column_List" />
  	from user
    where username = #{value}
  </select>
</mapper>

Form 7: Create UserService Impl and UserService
I skipped the code for UserService to take up space.

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserMapper userMapper;
	
	@Override
	public User queryUserByUserName(String username) {
		return userMapper.queryUserByUserName(username);
	}
}

Form 8: Create User Controller
In the same way, the shelves are laid out here first, and then we fill in the things we need slowly. This is a way of thinking that we must have in the actual development. Otherwise, if we think of one to write, and then to write another, the idea will be very confusing.

package com.marco.controller;

import java.util.HashMap;
import java.util.Map;

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

@RestController
@RequestMapping("user")
public class UserController {
	
	@RequestMapping("query")
	public Map<String,Object> query() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", "query");
		return map;
	}
	
	@RequestMapping("add")
	public Map<String,Object> add() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", "add");
		return map;
	}
	
	@RequestMapping("update")
	public Map<String,Object> update() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", "update");
		return map;
	}
	
	@RequestMapping("delete")
	public Map<String,Object> delete() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", "delete");
		return map;
	}
	
	
	@RequestMapping("export")
	public Map<String,Object> export() {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("msg", "export");
		return map;
	}
}

Form 9: Create RoleMapper and modify RoleMapper.xml
Like UserMapper and UserMapper.xml, I'm still only making modifications here, because the user's roles are queried for permission and role management later, so we write this method in RoleMapper, and query all the roles of the user (one-to-many relationship) through the user's ID.

package com.marco.mapper;

import java.util.List;

import com.marco.domain.Role;

public interface RoleMapper {
    int deleteByPrimaryKey(Integer roleid);

    int insert(Role record);

    int insertSelective(Role record);

    Role selectByPrimaryKey(Integer roleid);

    int updateByPrimaryKeySelective(Role record);

    int updateByPrimaryKey(Role record);
    
    //Query the user's role based on the user's id
    List<Role> queryRolesByUserId(Integer userid);
}

Modify RoleMapper.xml

<mapper namespace="com.marco.mapper.RoleMapper">
  <resultMap id="BaseResultMap" type="com.marco.domain.Role">
    <id column="roleid" jdbcType="INTEGER" property="roleid" />
    <result column="rolename" jdbcType="VARCHAR" property="rolename" />
  </resultMap>
  <sql id="Base_Column_List">
    roleid, rolename
  </sql>
  <!-- According to the user's id Query the roles of users -->
  <select id="queryRolesByUserId" resultMap="BaseResultMap">
  	 select t1.* from role t1 inner join user_role t2 
  	 on (t1.roleid = t2.roleid) 
  	 where t2.userid = #{value}
  </select>
</mapper>

Form 10: Create RoleService and RoleService Impl
Note that all we need to do here is to query the name of the user role.

package com.marco.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.marco.domain.Role;
import com.marco.mapper.RoleMapper;
import com.marco.service.RoleService;
@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	RoleMapper roleMapper;
	//Query the names of all roles owned by the user based on the user's id
	@Override
	public List<String> queryRolesByUserId(Integer userid) {
		List<Role> roleList = roleMapper.queryRolesByUserId(userid);
		List<String> roles = new ArrayList<String>();
		for (Role role : roleList) {
			roles.add(role.getRolename());
		}
		return roles;
	}
}	

Form 11: Create PermissionMapper and modify PermissionMapper.xml

package com.marco.mapper;

import java.util.List;

import com.marco.domain.Permission;

public interface PermissionMapper {
    int deleteByPrimaryKey(Integer perid);

    int insert(Permission record);

    int insertSelective(Permission record);

    Permission selectByPrimaryKey(Integer perid);

    int updateByPrimaryKeySelective(Permission record);

    int updateByPrimaryKey(Permission record);
    
    //Query all permissions you have based on the user's id
    List<Permission> queryPermissionByUserId(Integer userid);
}

The query PermissionByUserId (String userid) method requires a combination of three table queries

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.marco.mapper.PermissionMapper">
  <resultMap id="BaseResultMap" type="com.marco.domain.Permission">
    <id column="perid" jdbcType="INTEGER" property="perid" />
    <result column="pername" jdbcType="VARCHAR" property="pername" />
    <result column="percode" jdbcType="VARCHAR" property="percode" />
  </resultMap>
  <sql id="Base_Column_List">
    perid, pername, percode
  </sql>
  <!-- According to the user id Query User's Rights -->
  <select id="queryPermissionByUserId" resultMap="BaseResultMap">
  	 select t1.* from permission t1 inner join role_permission t2 inner join user_role t3 
  	 on (t1.perid = t2.perid and t2.roleid = t3.roleid) 
  	 where t3.userid = #{value}
  </select>
</mapper>

Twelfth Form: Create PermissionService and PermissionService Impl
Here we need to query not the name of Permission, but its percode, percode is the name of our corresponding operations (such as add, modify, delete, etc.), you can see the following data should be clear.

package com.marco.service.impl;

import java.util.ArrayList;
import java.util.List;

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

import com.marco.domain.Permission;
import com.marco.mapper.PermissionMapper;
import com.marco.service.PermissionService;

@Service
public class PermissionServiceImpl implements PermissionService{
	@Autowired
	PermissionMapper permissionMapper;
	//Query percode for all user rights based on user id
	@Override
	public List<String> queryPermissionByUserId(Integer userid) {
		List<Permission> permissionList = permissionMapper.queryPermissionByUserId(userid);
		List<String> permissions = new ArrayList<String>();
		for (Permission permission : permissionList) {
			permissions.add(permission.getPercode());
		}
		return permissions;
	}
}

Form 13: Create ActiverUser
ActiverUser is a container that stores user information, including basic information, roles, and privileges. We also mentioned the use and usage of ActiverUser in the previous chapters.

package com.marco.utils;

import java.util.List;

import com.marco.domain.User;

public class ActiveUser{
	private User user;
	
	private List<String> roles;
	
	private List<String> permissions;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<String> getRoles() {
		return roles;
	}

	public void setRoles(List<String> roles) {
		this.roles = roles;
	}

	public List<String> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<String> permissions) {
		this.permissions = permissions;
	}
}

So far, the basic part has been built, and the first 13 styles of the "Dragon Dropping 18" have been finished, tired............................................ Take a break at halftime. In the next section, we will build the core part of Shiro's integration with SMM. Keep going.~

Keywords: Spring Maven Mybatis Java

Added by djpeterlewis on Sun, 04 Aug 2019 10:29:23 +0300