一、web 项目的基本依赖
<!--
Jsp 编译使用
https
://mvnrepository
.com
/artifact
/javax
.servlet
/javax
.servlet
-api
-->
<dependency>
<groupId>javax
.servlet
</groupId
>
<artifactId>javax
.servlet
-api
</artifactId
>
<version>3.0.1</version
>
<scope>provided
</scope
>
</dependency
>
<!-- org
/apache
/commons
/fileupload
/FileItemFactory 因为springmvc 里面的文件上传解析器需要fileUpload
这个依赖
-->
<!-- https
://mvnrepository
.com
/artifact
/commons
-fileupload
/commons
-fileupload
-->
<dependency>
<groupId>commons
-fileupload
</groupId
>
<!-- 需要commons
-io的依赖
-->
<artifactId>commons
-fileupload
</artifactId
>
<version>1.3.1</version
>
</dependency
>
<!--
对象
->json
https
://mvnrepository
.com
/artifact
/com
.fasterxml
.jackson
.core
/jackson
-databind
-->
<dependency>
<groupId>com
.fasterxml
.jackson
.core
</groupId
>
<artifactId>jackson
-databind
</artifactId
>
<version>2.9.8</version
>
</dependency
>
二、数据库的设计
T_user
三、生成数据库对应的实体和mapper.xml文件
3.1 生成
3.2 将实体和mapper 接口导入到项目里面
3.2.1 实体
3.2.2 mapper 和xml 文件
四、依赖关系处理
4.1 内部依赖处理
Domain mapper api service web
Mapper 依赖domain Api 依赖domain Service 依赖api,mapper
${db.username} 代表spring 将从Environment 找到db.username 来注入 Environment 都来自哪里? 1 properties文件 2 来下系统的环境变量(user.name)=计算机的名称 3 来自控制台 --user.name=xx
五、问题的解决
5.1 mysql 驱动
<!-- https
://mvnrepository
.com
/artifact
/mysql
/mysql
-connector
-java
-->
<dependency>
<groupId>mysql
</groupId
>
<artifactId>mysql
-connector
-java
</artifactId
>
<version>5.1.47</version
>
</dependency
>
5.2 mybaits的配置文件
Mybatis.cfg.xml
<?xml version
="1.0" encoding
="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration
>
5.3 启动时发现没有任何问题,但是一直不动
是数据库连接的错误,检查
db
.username
=root
db
.password
=123456
db
.url
=jdbc
:mysql
://192.168.231.143:3310/user
db
.driverClassName
=com
.mysql
.jdbc
.Driver
六、外部的依赖处理
6.0 user-parent pom.xml
<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
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
<packaging>pom
</packaging
>
<name>user父项目
</name
>
<description>user父项目,约束版本
</description
>
<!-- 约束版本的
-->
<!-- ssm
-->
<properties>
<spring.version>4.3.16.RELEASE
</spring
.version
>
<mybaites.version>3.4.1</mybaites
.version
>
<spring
-mybaits
.version
>1.3.2</spring
-mybaits
.version
>
<dubbo.version>2.6.2</dubbo
.version
>
<zkclient.version>0.11</zkclient
.version
>
<druid.version>1.1.10</druid
.version
>
<mysql.version></mysql
.version
>
</properties
>
<!-- dependencyManagement 声明依赖,并没有依赖进来
-->
<dependencyManagement>
<dependencies>
<!-- https
://mvnrepository
.com
/artifact
/org
.springframework
/spring
-context
-->
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-context
</artifactId
>
<version>$
{spring
.version
}</version
>
</dependency
>
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-jdbc
</artifactId
>
<version>$
{spring
.version
}</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.springframework
/spring
-webmvc
-->
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-webmvc
</artifactId
>
<version>$
{spring
.version
}</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.mybatis
/mybatis
-->
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
</artifactId
>
<version>$
{mybaites
.version
}</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.mybatis
/mybatis
-spring
-->
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
-spring
</artifactId
>
<version>$
{spring
-mybaits
.version
}</version
>
</dependency
>
<!-- dubbo
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.alibaba
/dubbo
-->
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>dubbo
</artifactId
>
<version>$
{dubbo
.version
}</version
>
</dependency
>
<!-- zkclient
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.101tec
/zkclient
-->
<dependency>
<groupId>com
.101tec
</groupId
>
<artifactId>zkclient
</artifactId
>
<version>$
{zkclient
.version
}</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/com
.alibaba
/druid
-->
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>druid
</artifactId
>
<version>$
{druid
.version
}</version
>
</dependency
>
</dependencies
>
</dependencyManagement
>
<!-- <build> -->
<!-- <pluginManagement> -->
<!-- <plugins> -->
<!-- <plugin> -->
<!-- tomcat 的插件
-->
<!-- </plugin
> -->
<!-- </plugins
> -->
<!-- </pluginManagement
> -->
<!-- </build
> -->
<modules>
<module>user
-domain
</module
>
<module>user
-mapper
</module
>
<module>user
-api
</module
>
<module>user
-service
</module
>
<module>user
-web
</module
>
</modules
>
</project
>
6.1 user-domain pom.xml
<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
>
<parent>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
</parent
>
<artifactId>user
-domain
</artifactId
>
<name>user的实体模型
</name
>
<description>user的实体模型
</description
>
</project
>
6.2 user-mapper pom.xml
<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
>
<parent>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
</parent
>
<artifactId>user
-mapper
</artifactId
>
<name>user的数据库操作对象
</name
>
<dependencies>
<dependency>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-domain
</artifactId
>
<version>1.0</version
>
</dependency
>
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
</artifactId
>
</dependency
>
</dependencies
>
</project
>
6.3 user-service pom.xml
<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
>
<parent>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
</parent
>
<artifactId>user
-service
</artifactId
>
<name>用户数据接口的实现类
</name
>
<description>用户数据接口的实现类
</description
>
<dependencies>
<dependency>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-api
</artifactId
>
<version>1.0</version
>
</dependency
>
<dependency>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-mapper
</artifactId
>
<version>1.0</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.springframework
/spring
-context
-->
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-context
</artifactId
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.mybatis
/mybatis
-->
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
</artifactId
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.mybatis
/mybatis
-spring
-->
<dependency>
<groupId>org
.mybatis
</groupId
>
<artifactId>mybatis
-spring
</artifactId
>
</dependency
>
<!-- dubbo
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.alibaba
/dubbo
-->
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>dubbo
</artifactId
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/mysql
/mysql
-connector
-java
-->
<dependency>
<groupId>mysql
</groupId
>
<artifactId>mysql
-connector
-java
</artifactId
>
<version>5.1.47</version
>
</dependency
>
<!-- zkclient
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.101tec
/zkclient
-->
<dependency>
<groupId>com
.101tec
</groupId
>
<artifactId>zkclient
</artifactId
>
</dependency
>
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>druid
</artifactId
>
</dependency
>
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-jdbc
</artifactId
>
</dependency
>
</dependencies
>
</project
>
6.4 user-api pom.xml
<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
>
<parent>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
</parent
>
<artifactId>user
-api
</artifactId
>
<name>user操作数据接口
</name
>
<description>user操作数据接口
</description
>
<dependencies>
<dependency>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-domain
</artifactId
>
<version>1.0</version
>
</dependency
>
</dependencies
>
</project
>
6.5 user-web pom.xml
<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
>
<parent>
<groupId>com
.sxt
</groupId
>
<artifactId>user
-parent
</artifactId
>
<version>1.0</version
>
</parent
>
<artifactId>user
-web
</artifactId
>
<packaging>war
</packaging
>
<name>user
-web
</name
>
<description>user
-web
</description
>
<dependencies>
<!-- https
://mvnrepository
.com
/artifact
/javax
.servlet
/javax
.servlet
-api
-->
<dependency>
<groupId>javax
.servlet
</groupId
>
<artifactId>javax
.servlet
-api
</artifactId
>
<version>3.0.1</version
>
<scope>provided
</scope
>
</dependency
>
<!-- org
/apache
/commons
/fileupload
/FileItemFactory 因为springmvc 里面的文件上传解析器需要fileUpload
这个依赖
-->
<!-- https
://mvnrepository
.com
/artifact
/commons
-fileupload
/commons
-fileupload
-->
<dependency>
<groupId>commons
-fileupload
</groupId
>
<!-- 需要commons
-io的依赖
-->
<artifactId>commons
-fileupload
</artifactId
>
<version>1.3.1</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/com
.fasterxml
.jackson
.core
/jackson
-databind
-->
<dependency>
<groupId>com
.fasterxml
.jackson
.core
</groupId
>
<artifactId>jackson
-databind
</artifactId
>
<version>2.9.8</version
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.springframework
/spring
-context
-->
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-context
</artifactId
>
</dependency
>
<!-- https
://mvnrepository
.com
/artifact
/org
.springframework
/spring
-webmvc
-->
<dependency>
<groupId>org
.springframework
</groupId
>
<artifactId>spring
-webmvc
</artifactId
>
</dependency
>
<!-- dubbo
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.alibaba
/dubbo
-->
<dependency>
<groupId>com
.alibaba
</groupId
>
<artifactId>dubbo
</artifactId
>
</dependency
>
<!-- zkclient
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.101tec
/zkclient
-->
<dependency>
<groupId>com
.101tec
</groupId
>
<artifactId>zkclient
</artifactId
>
</dependency
>
</dependencies
>
<build>
<plugins>
<plugin>
<!-- tomcat7 插件
-->
<groupId>org
.apache
.tomcat
.maven
</groupId
>
<artifactId>tomcat7
-maven
-plugin
</artifactId
>
<version>2.2</version
>
<configuration>
<path>/</path
>
<port>8080</port
>
<uriEncoding>UTF
-8</uriEncoding
>
</configuration
>
</plugin
>
</plugins
>
</build
>
</project
>
七、完成user的curd
7.1 user-api新增接口
public interface UserService {
User
insertUser(User user
);
Integer
deleteUser(Integer uid
);
Integer
updateUser(User user
);
User
findById(Integer id
);
List
<User> findByPage(int size
,int page
);
}
7.2 user-service 实现该接口
@Service
public class UserServiceImpl implements UserService{
private static Logger log
= LoggerFactory
.getLogger(UserServiceImpl
.class);
@Autowired
private UserMapper userMapper
;
@Override
public User
insertUser(User user
) {
Assert
.notNull(user
, "新增用户不能为null");
log
.info("{新增用户}"+user
);
userMapper
.insert(user
);
return user
;
}
@Override
public Integer
deleteUser(Integer uid
) {
Assert
.notNull(uid
, "删除用户时id不能为空");
log
.info("{删除用户}"+uid
);
int result
= userMapper
.deleteByPrimaryKey(uid
);
return result
;
}
@Override
public Integer
updateUser(User user
) {
Assert
.notNull(user
, "修改的用户不能为空");
Assert
.notNull(user
.getId(), "修改用户时id不能为null");
log
.info("{修改用户}"+user
);
int result
= userMapper
.updateByPrimaryKeySelective(user
);
return result
;
}
@Override
public User
findById(Integer id
) {
Assert
.notNull(id
, "查询的用户不能为空");
log
.info("{查询用户}"+id
);
return userMapper
.selectByPrimaryKey(id
);
}
@Override
public List
<User> findByPage(int size
, int page
) {
return null
;
}
}
7.3 user-service 启动该项目
public class UserApp {
public static void main(String
[] args
) {
ClassPathXmlApplicationContext app
= new ClassPathXmlApplicationContext("classpath:spring-*.xml");
app
.start();
UserService userService
= app
.getBean(UserService
.class);
User user
= userService
.findById(1);
System
.out
.println(user
.getUsername());
}
}
7.4 分页的实现
<!-- 分页插件分页插件
-->
<!-- https
://mvnrepository
.com
/artifact
/com
.github
.pagehelper
/pagehelper
-->
<dependency>
<groupId>com
.github
.pagehelper
</groupId
>
<artifactId>pagehelper
</artifactId
>
<version>4.1.6</version
>
</dependency
>
7.4.1 添加插件
Mybatis.cfg/xml
<?xml version
="1.0" encoding
="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor
="com.github.pagehelper.PageHelper"></plugin
>
</plugins
>
</configuration
>
7.4.2 拦截sql
UserServiceImpl
八、处理controller 和service 之间的关系
在controller 里面必须依赖service 才行实现curd 但是现在我们需要使用远程调用来实现?
8.1 使用暴露注解和引用远程服务的注解
UserController UserServiceImpl
8.2 dubbo的配置文件
8.2.1 提供者 Spring-dubbo-provider.xml
<beans xmlns
="http://www.springframework.org/schema/beans"
xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns
:dubbo
="http://dubbo.apache.org/schema/dubbo"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans http
://www
.springframework
.org
/schema
/beans
/spring
-beans
-4.3.xsd
http
://dubbo
.apache
.org
/schema
/dubbo http
://dubbo
.apache
.org
/schema
/dubbo
/dubbo
.xsd
http
://code
.alibabatech
.com
/schema
/dubbo http
://code
.alibabatech
.com
/schema
/dubbo
/dubbo
.xsd"
>
<!-- 应用名称
-->
<dubbo
:application name
="user-service" />
<!-- 注册中心
-->
<dubbo
:registry protocol
="zookeeper" address
="${zk.url}"
client
="zkclient" />
<!-- 暴露的端口
-->
<dubbo
:protocol port
="8888" />
<!-- 包扫描
-->
<dubbo
:annotation
package="com.sxt.service.impl" />
</beans
>
同时在db.properties 里面添加 启动zk 就可以测试了
8.2.2 消费者
Dubbo-consmuer.xml
<beans xmlns
="http://www.springframework.org/schema/beans"
xmlns
:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns
:dubbo
="http://code.alibabatech.com/schema/dubbo"
xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beans http
://www
.springframework
.org
/schema
/beans
/spring
-beans
-4.3.xsd
http
://dubbo
.apache
.org
/schema
/dubbo http
://dubbo
.apache
.org
/schema
/dubbo
/dubbo
.xsd
http
://code
.alibabatech
.com
/schema
/dubbo http
://code
.alibabatech
.com
/schema
/dubbo
/dubbo
.xsd"
>
<!-- 1 应用名称
-->
<!-- 2 注册中心
-->
<!-- 3 包扫描
-->
<dubbo
:application name
="user-web" />
<dubbo
:registry protocol
="zookeeper" address
="192.168.231.143:2181" client
="zkclient"> </dubbo
:registry
>
<dubbo
:annotation
package="com.sxt.controller"/>
</beans
>
同时在springmvc.xml 里面引用该文件
8.2.3 在controller 里面调用Service
package com
.sxt
.controller
;
import java
.util
.HashMap
;
import java
.util
.Map
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.PostMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
import com
.alibaba
.dubbo
.config
.annotation
.Reference
;
import com
.sxt
.domain
.User
;
import com
.sxt
.service
.UserService
;
@Controller
public class UserController {
@Reference
private UserService userService
;
@RequestMapping("/toMain")
public String
toMain() {
return "main";
}
@RequestMapping("/json")
@ResponseBody
public Object
getJson() {
Map
<String,Object> result
= new HashMap<>();
result
.put("code",200);
result
.put("msg","OK");
return result
;
}
@PostMapping("/user/add")
@ResponseBody
public User
addUser(User user
) {
return userService
.insertUser(user
);
}
@PostMapping("/user/del")
@ResponseBody
public Object
delUser(Integer id
) {
Integer result
= userService
.deleteUser(id
);
if(result
>0) {
return "OK";
}else {
return "FAIL";
}
}
}
九、使用Postman测试数据接口
9.1 能进google的应用商店
需要google 的访问助手才能进 下载 解压 Assist的文件夹 导入
9.2 下载postman的工具
9.3 使用postman测试数据接口
十、远程调用的本质
Redis 共享内存的做法 Zookeeper 也是共享内存的一种 MQ 也是共享内存的一种