Maven+Dubbo+Mybatis+SpringBoot构建JavaWeb项目

SpringBoot灵活的配置减少了很多麻烦,首先要理解各个模块的作用。ZooKeeper是一个注册中心,其中我们把调用了dao层的service层的实现给注册到ZooKeeper中,然后在web调用rpc模块里的service接口就可以使用service了。

首先创建一个数据库,建一个user表,字段包括id,username, password

1. 首先构建maven父项目,这里我取名MyDemo,它包含3个子项目MyDemo-app(相当于dao层和service层)、MyDemo-rpc(包含数据传输对象dto和供web层调用的service接口)、MyDemo-web(Web项目)

父项目的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.rambo</groupId>
  <artifactId>MyDemo</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  
  <dependencyManagement>
		<dependencies>
			<dependency>
				<!-- Import dependency management from Spring Boot -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.0.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<dependencies>
		<!-- 一个减少冗余代码的插件 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<!--  <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>-->		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<modules>
		<module>MyDemo-rpc</module>
		<module>MyDemo-app</module>
		<module>MyDemo-web</module>
	</modules>
	
</project>

2. 创建MyDemo-app子模块,这里用到了mybatis逆向工程生成domain和mapper文件

①先写pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<artifactId>MyDemo-app</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>MyDemo-app</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.rambo</groupId>
		<artifactId>MyDemo</artifactId>
		<version>1.0</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.rambo</groupId>
			<artifactId>MyDemo-rpc</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.9</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.12</version>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<configuration>
					<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.46</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>


</project>

②写mybatis的生成文件:generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
		<!--执行generator插件生成文件的命令: call mvn mybatis-generator:generate -e -->
	    <!-- 引入配置文件 -->
	    <!-- <properties resource="mybatis-generator/mybatisGeneratorinit.properties"/> -->
	    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
	    <!--<classPathEntry location="D:\generator_mybatis\mysql-connector-java-5.1.24-bin.jar" /> -->
	    
	<!-- 一个数据库一个context -->
    <!--defaultModelType="flat" 大数据字段,不分表 -->    
	<context id="context1">
		<!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
        一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true" />
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8" />
        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />
        
        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
 
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        
        
		<commentGenerator>
			<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
		</commentGenerator>
		
		<!-- jdbc连接 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/MyDemo?useUnicode=true&amp;characterEncoding=utf-8"
			userId="root" password="123456">
		</jdbcConnection>
		<!-- 类型转换 -->
		<javaTypeResolver>
			<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成的实体包路径,包含实体类和Example类(用来定制sql的) -->
		<javaModelGenerator targetPackage="com.rambo.MyDemo.app.domain"
			targetProject="src/main/java" />
		<!-- 生成的Mapper.xml路径 -->
		<sqlMapGenerator targetPackage="mybatis-mapping"
			targetProject="src/main/resources" />
		<!-- 生成的mapper类 -->
		<javaClientGenerator targetPackage="com.rambo.MyDemo.app.dao"
			targetProject="src/main/java" type="XMLMAPPER" />
		
		<!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,-->
		<!-- 也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
		<table schema="" tableName="user" domainObjectName="User"
			enableCountByExample="true" enableUpdateByExample="true"
			enableDeleteByExample="true" enableSelectByExample="true"
			selectByExampleQueryId="true">
			<property name="useActualColumnNames" value="false" />
			<generatedKey column="id" sqlStatement="JDBC" identity="true" />
		</table>
 
	</context>
</generatorConfiguration>

③先maven->update project,然后maven build输入mybatis-generator:generate生成domain和mapper等文件

④创建application.yml配置数据库和Dubbo

spring:
  devtools:
    livereload:
      port: 35729
  datasource:
    url: jdbc:mysql://localhost:3306/MyDemo?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    druid:
      
      filter:
        config:
          enabled: true
  application:
    name: dubbo-demo-provider
  dubbo:
    registry:
      address: zookeeper://127.0.0.1:2181
    server: true
    protocol:
      port: 20880
      threadpool: cached
      threads: 300
      accepts: 500
      serialization: java
mybatis:
  mapperLocations: mybatis-mapping/**Mapper.xml

⑤创建MybatisConfiguration.java来扫描生成的mapper文件

 

package com.rambo.MyDemo.app.config;

import javax.annotation.PostConstruct;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class, MybatisAutoConfiguration.class })
@MapperScan(MybatisConfiguration.BASE_MAPPER_PACKAGE)
public class MybatisConfiguration {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	protected static final String BASE_MAPPER_PACKAGE = "com.rambo.MyDemo.app.dao";

	@PostConstruct
	public void init() {
		logger.info("加载" + BASE_MAPPER_PACKAGE + "路径下的Mapper");
	}

}

 ⑥创建启动类

package com.rambo.MyDemo.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

@SpringBootApplication
@EnableDubboConfiguration
@EnableCaching
@EnableTransactionManagement
public class MyDemoAppApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyDemoAppApplication.class, args);
	}
}

3.创建MyDemo-rpc子模块maven项目,其作用是前端和后端的一个媒介以及web端调用service的接口

①pom.xml

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.rambo</groupId>
		<artifactId>MyDemo</artifactId>
		<version>1.0</version>
	</parent>
	<artifactId>MyDemo-rpc</artifactId>
	<name>MyDemo-rpc</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

</project>

②创建dto对象,和生成的User一样

③创建UserService接口

package com.rambo.MyDemo.rpc.iservice;

public interface UserService  {

	 Object login(int userId, String password);
}

④在MyDemo-app模块里实现UserService接口

package com.rambo.MyDemo.app.service;



import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;

import com.rambo.MyDemo.app.dao.UserMapper;
import com.rambo.MyDemo.app.domain.User;
import com.rambo.MyDemo.app.domain.UserExample;
import com.rambo.MyDemo.app.domain.UserExample.Criteria;
import com.rambo.MyDemo.rpc.iservice.UserService;

@Service(interfaceClass=UserService.class)
@Component
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserMapper userMapper;
	
	public Object login(int userId, String password) {
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		
			criteria.andIdEqualTo(userId);		
		if (StringUtils.isNotEmpty(password)) {
			criteria.andPasswordEqualTo(password);
		}
		List<User> userList = userMapper.selectByExample(userExample);
		if (userList.size() > 0) {
			User user = userList.get((0));
			com.rambo.MyDemo.rpc.dto.User dtoUser = new com.rambo.MyDemo.rpc.dto.User();
			BeanUtils.copyProperties(user, dtoUser);			
				return dtoUser;
			
		}
		return null;
	}

}

 4. 创建MyDemo-web子模块

1.编写controller

package com.rambo.MyDemo.web.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

import com.alibaba.dubbo.config.annotation.Reference;
import com.rambo.MyDemo.rpc.dto.User;
import com.rambo.MyDemo.rpc.iservice.UserService;

@RestController
public class LoginController {

	@Reference
	private UserService userService;
	
	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public Map<String, String> adminlogin(HttpServletRequest request, User user) throws Exception {
		
		User user1 = (User) userService.login(user.getId(), user.getPassword());
		Map<String, String> map = new HashMap<String, String>();
		if (user1 != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("user", user1);
			map.put("message", "success");
			map.put("name", user1.getName());
			
		} else {
			map.put("message", "fail");
		}
		return map;
	}
	
    @RequestMapping(value="/checkUser",method = RequestMethod.POST)
    public String checkUser(HttpServletRequest request,HttpServletResponse response){
        //编码规范
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //获取session值
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println(user.getName());
        return user.getName();
       
    }

}

②编写配置文件application.yml

spring:
  devtools:
    livereload:
      port: 35728
  application:
    name: dubbo-demo-web
  dubbo:
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      port: 20880
      threadpool: cached
      threads: 300
      accepts: 500
      serialization: java
  cache:
    redis:
      key-prefix: dubbo-demo-provider-cache-
      time-to-live: 10s
  redis:
    host: 127.0.0.1
    port: 6379
    timeout: 3s
  mvc:
    static-path-pattern: /**
    resources:
        static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/
shiro:
  loginUrl: ${shiro.casServerUrlPrefix}?service=${shiro.webUrl}
  successUrl: null
  webUrl: http://127.0.0.1:8080/demo/shiro-cas
  unauthorizedUrl: ${shiro.casServerUrlPrefix}?service=${shiro.webUrl}
  casServerUrlPrefix: http://10.79.10.21:8080/cas
server:
  port: 8080
  servlet:
    context-path: /MyDemo
    

 ③编写前端页面

<!DOCTYPE html>
<html lang="zh">

<head>    
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="../components/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="../js/ajax.js"></script>
    <script type="text/javascript" src="../common/js/store.min.js"></script>
</head>

<body>
<h3>登录页面</h3>
<form >
	<font color="red" >${requestScope.message}</font>
	<table>
		<tr>
			<td><label>用户ID:</label></td>
			<td><input type="text" id="id" name="userId" value="1000"/></td>
		</tr>
		<tr>
			<td><label>密码:</label></td>
			<td><input type="password" id="password" name="password" value="123456"/></td>
		</tr>
		<tr>
			<td><button name="login" type="button" id="login" >登录</button></td>
		</tr>
	</table>
</form>
</body>
<script>
    $(document).ready(function() {
    	
    	$("#login").click(function() {
    		
    		var userId = $("#id").val();
    		var password = $("#password").val();
    		console.log(userId+" "+password);
    		if (userId == "" || password == "") {
    			alert("用户名和密码不可以为空!");
    		} else {
    			var param = new Object();
   				param.id = userId;
   				param.password = password;
   				loginFun("../login", param);
    			
    		}
    	});
    });

    function loginFun(urls,param) {
    	$.ajax({
    		type : "POST",
    		url : urls,
    		data : param,
    		dataType : "json",
    		contentType: "application/x-www-form-urlencoded",
    		success : function(data,textStatus,jqXHR) {
    			console.log(data,);
    			console.log(textStatus);
    			console.log(jqXHR);
    			if (data.message != "fail") {
    				
    				alert("登陆成功");
    				window.location = 'home.html';
    			}else{
    				alert("账号密码错误");
    			}
    		}
    	});
    }
    </script>
</html>

④配置SPringBoot启动类

package com.rambo.MyDemo.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.rambo.MyDemo.web.DemoWebApplication;

@SpringBootApplication
@EnableDubboConfiguration
@EnableCaching
@ServletComponentScan
public class DemoWebApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoWebApplication.class, args);
	}

}

运行先运行ZooKeeper,再以JavaAplication方式启动MyDemo-app里的类,再以SpringBoot方式启动MyDemo-web里的类,访问localhost:8080/MyDemo/login.html即可

项目地址

猜你喜欢

转载自blog.csdn.net/qq_33655674/article/details/82709671