互联网框架阶段:
maven的配置pom(配置路径以及镜像)
创建一个Java项目
mvn archetype:generate
网址学习:
https://www.kchuhai.com/report/view-11509.html(配置)
https://blog.youkuaiyun.com/wangkiing_csdn/article/details/51913585(讲解全)
解压maven安装包到没有中文路径的盘符下,maven3.6配jdk1.8,eclipse使用neon
需要用到的包:
easymall的整改:
购物车订单用户的搜索秒杀商品的功能的一个高可用的分布式的微服务的集群
maven:工具技术,管理项目
ssm框架:demo系统,(html+ajax)
maven的原理:
maven库的概念:
配置本地的ali_repo
休要修改maven根目录conf文件下的settings.xml
maven的常用指令:
资源依赖:
资源的结构:
生命周期
cmd窗口的操作
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\16902>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
some of the supported document types like APT, XDoc, and FML and demonstrates how
to i18n your site. This archetype can be layered upon an existing Maven project.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
Define value for property 'groupId': e
Define value for property 'artifactId': e
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' e: :
Confirm properties configuration:
groupId: e
artifactId: e
version: 1.0-SNAPSHOT
package: e
Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\16902
[INFO] Parameter: package, Value: e
[INFO] Parameter: groupId, Value: e
[INFO] Parameter: artifactId, Value: e
[INFO] Parameter: packageName, Value: e
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\16902\e
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 51:15 min
[INFO] Finished at: 2021-02-10T16:05:18+08:00
[INFO] ------------------------------------------------------------------------
maven项目文件里的编码文件:
项目的编译:
mvn clean compile
项目的清理:
mvn clean
eclipse和maven的整合
eclipse的使用:
5.1环境调试
jdk1.8
引入maven:
创建一个maven的工程:(普通的maven【quickstart】)
补充缺少的文件:
maven的常用操作:
依赖的传递性(多级传递性)
maven与ssm框架的整合(创建webapp骨架)
maven工程的常见问题解决:
关于maven报错无法创建项目(webapp骨架)
将阿里镜像关闭使用其它的镜像(上述的镜像不可用)或者用原始的默认镜像,重新创建项目eclipse会自动的下载
其它的解决方案:
自建一个项目空间(other project是eclipse自带的)
webapp提示的错误(index.heml文件报错)
问题描述:默认创建的webapp骨架会使用eclipse对前端开发文件的校验
解决的方法:
将eclipse校验关闭
window—>preferences—>validation
无缘无故的红叉子
解决方法:
执行maven的更新 右键项目:maven—>updat project
加入缺少的文件夹
开始编写
1.1依赖的实现:
连接池数据源:(druid)
spring-jdbc(提供的datasource接口)
mysql连接
mybatis的配置
1.2配置spring.xml整合持久层
1.3测试代码(流程)
代码实现:
package cn.tedu.domain;
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
public User(Integer id, String username, String password, String nickname, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", nickname=" + nickname
+ ", email=" + email + "]";
}
}
package cn.tedu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import cn.tedu.domain.User;
import cn.tedu.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
//通过id查询一个用户的user对象
public User queryOneById(Integer id){
return userService.queryOne(id);
}
}
package cn.tedu.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.domain.User;
import cn.tedu.mapper.UserMapper;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryOne(Integer id) {
return userMapper.selectUserById(id);
}
}
package cn.tedu.mapper;
import org.springframework.stereotype.Repository;
import cn.tedu.domain.User;
@Repository
public interface UserMapper {
User selectUserById(Integer id);
}
测试:
package cn.tedu.spring.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.tedu.controller.HelloController;
import cn.tedu.controller.UserController;
import cn.tedu.domain.User;
import cn.tedu.mapper.HelloDao;
import cn.tedu.service.HelloService;
/**
* 实现加载spring.xml上下文对象
* 创建成功,获取controller service
* @author 在路上
*
*/
public class SpringTest {
//classpath:上下文对象使用读取xml文件的固定写法
//不一样的工程读取的位置不同
//maven target/classes/
@Test
public void test01(){
ClassPathXmlApplicationContext context
=new ClassPathXmlApplicationContext("classpath:spring.xml");
//通过加载的配置文件获取对象
HelloController controller = context.getBean(HelloController.class);
HelloService service = context.getBean(HelloService.class);
HelloDao dao = context.getBean(HelloDao.class);
System.out.println(controller.sayHello("make"));
System.out.println(service.sayHello("wangcai"));
System.out.println(dao.sayHello("jiahuai"));
}
@Test
public void test02(){
ClassPathXmlApplicationContext context
=new ClassPathXmlApplicationContext("classpath:spring.xml");
//创建一个controller传递一个id 返回user对象
UserController controller = context.getBean(UserController.class);
User user=controller.queryOneById(2);
System.out.println(user);
}
}
配置:
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--mysql 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<!--德鲁伊连接数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
<!--mybatis 相关的两个配置 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:P="http://www.springframework.org/schema/P"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/P
http://www.springframework.org/schema/P/spring-P-3.2.xsd
" >
<!--扫描的基础路径cn.tedu 扫描所有以cn.tedu开始的包路径 -->
<!--扫描的基础路径cn.tedu.web.service -->
<context:component-scan base-package="cn.tedu"/>
<!--准备一个数据源对象 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- url -->
<property name="url" value="jdbc:mysql:///easymall"></property>
<!-- 注册驱动类 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 登录数据库的用户名和密码 -->
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- sqlsession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--导入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!--mybatis的配置文件 准备出来 否则无法加载 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--mapper映射文件的扫描 扫描使用*通配 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<!--别名包 resultType domain entity dto vo pojo -->
<property name="typeAliasesPackage" value="cn.tedu.domain"></property>
</bean>
<!--扫描动态代理调用sqlsession -->
<!--实现接口类的方法 提供接口类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描的包路径 cn.tedu.mapper -->
<property name="basePackage" value="cn.tedu.mapper"></property>
</bean>
</beans>
<?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>
<?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="cn.tedu.mapper.UserMapper">
<select id="selectUserById" parameterType="int" resultType="User">
select * from user where id=#{id};
</select>
</mapper>