在eclipse中使用Maven搭建SSM项目
在eclipse中使用Maven搭建SSM项目
在eclipse中使用Maven搭建SSM项目
一、 搭建maven
- 安装maven
第一步:Apache Maven官网:http://maven.apache.org/
第二步:不想下载最新版的话,可以下载旧版
第三步:选择合适的版本,我用的是3.3.9
第四步:解压zip文件
- 配置环境
第一步:右击我的电–>属性–>高级系统设置–>环境变量–>系统变量–>新建
第二部:再配置Path变量
跟配置java环境是一样的,最后几个一直点确定就行。 home路径要自己定一个,不能和其他重复。路径最好是拷贝,手动输入太容易出错了。
第三步:检验配置是否成功,打开命令行,输入mvn -v
成功!!!
二、 创建Maven项目
- 新建Maven Object项目
File–>New -->Maven project
定义名称
项目结构图
2. 配置maven
Window—>Preferences—>Maven
配置Installations
配置User Settings
3. 导入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.czxy</groupId>
<artifactId>Demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 编码版本、jdk, -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
<mybatis.starter.version>1.3.2</mybatis.starter.version>
<mapper.starter.version>2.0.3</mapper.starter.version>
<pageHelper.starter.version>1.2.3</pageHelper.starter.version>
<druid.starter.version>1.1.9</druid.starter.version>
<mysql.version>5.1.32</mysql.version>
<fastDFS.client.version>1.26.1-RELEASE</fastDFS.client.version>
<jaxb.version>2.3.0</jaxb.version>
<fastjson.version>1.2.47</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.starter.version}</version>
</dependency>
<!-- 通用Mapper启动器 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>${mapper.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- druid启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.starter.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 引入Spring Boot 测试模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</plugins>
</build>
</project>
三、 创建Java类
- Controller类
package com.czxy.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.czxy.domain.User;
import com.czxy.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
List<User> list=userService.findAll();
System.out.println(list);
return list;
}
}
2.Service类
package com.czxy.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.czxy.dao.UserMapper;
import com.czxy.domain.User;
@Service
@Transactional
public class UserService {
@Resource
private UserMapper userDao;
public List<User> findAll(){
List<User> list=userDao.selectAll();
System.out.println(list);
return list;
}
}
- Dao类
package com.czxy.dao;
import com.czxy.domain.User;
import tk.mybatis.mapper.common.Mapper;
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<User>{
}
4.实体类
package com.czxy.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "tb_user")
public class User {
@Id
private Integer id;
private String name;
private String password;
private String mobile;
private String email;
//无参
public User() {
super();
// TODO Auto-generated constructor stub
}
//有参
public User(Integer id, String name, String password, String mobile, String email) {
super();
this.id = id;
this.name = name;
this.password = password;
this.mobile = mobile;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
//toString
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + ", mobile=" + mobile + ", email="
+ email + "]";
}
}
数据库
- 启动类
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(ApplicationDemo.class, args);
}
}
6.application.yaml文件
server:
port: 8080 #端口号
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/shop
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 1000
test-on-borrow: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.czxy.domain
7.配置tomcat
进行校验
运行启动类
浏览器访问
console控制台打印