需要技术
java,MySql基本使用,mybatis技术的了解,maven基本安装使用,SpringBoot使用,git的使用,idea使用基础,mvc结构。
Java基础
具备Java基本使用+接口使用+面向对象思想+注解
MySql使用
安装数据库+进入MySql+基本SQL语句
mybatis
基本的配置和理解
maven
安装maven+配置maven+基础命令
SpringBoot
了解作用+基本的创建+基本的配置
git
了解git+创建项目+基本命令
idea
安装+配置+链接git,配置maven,创建SpringBoot项目
mvc
基本结构+思路清晰
代码实现
创建项目
- idea旗舰版和社区版的区别
旗舰版的是Springboot,社区版的是spring Assistant
参考链接:https://blog.csdn.net/crazilyer/article/details/80313323
- 导入依赖包
1.勾选SQL中的JDBC API、MyBatis Framework、MySQL Driver,创建项目后就会自动配置和引入这些包。
2.在pom.xml文件中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.5</version>
</dependency>
注:如果maven包加载过程中取消了,可能会导致报错,执行mvn package重新加载
添加MybatisConfig类在新建config包下
package com.example.demo.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper",
sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfig {
static final String MAPPER_LOCATION = "classpath:mybatis/*.xml";
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
throws Exception {
final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MAPPER_LOCATION));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "dataSource")
public DataSource dataSource() {
DataSourceBuilder dataSource = DataSourceBuilder.create();
dataSource.driverClassName(driverClass);
dataSource.url(url);
dataSource.username(user);
dataSource.password(password);
return dataSource.build();
}
}
创建数据库
数据库名:spring_boot_demo
数据库表:t_user
字段:id,username,password,age,sex
配置数据库连接信息
springboot中默认的配置文件是application.properties,修改后缀名为application.yml,打开编辑配置信息
1.配置端口路径
server:
port: 8080
servlet:
context-path: /cn
2.配置数据库连接信息
spring:
datasource:
url: jdbc:mysql://localhost:3306/spring_boot_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
建立mvc架构实现
数据访问层
业务逻辑层
页面展示层
数据访问层
User类implements了一个Serializable类,该类的作用是序列化。
package com.example.demo.po;
import java.io.Serializable;
public class UserPO implements Serializable {
/** 用户ID */
private int id;
/** 用户名 */
private String username;
/** 用户密码 */
private String password;
/** 年龄 */
private int age;
/** 性别 */
private String sex;
public int getId() {
return id;
}
public void setId(int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@Repository注解是把这个接口的一个实现类交给spring管理
UserMapper接口:
package com.example.demo.mapper;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper {
Integer getAgeByUsername(String username);
}
UserMapper.xml配置文件:
mapper的namespace指定了该xml文件指向的Mapper接口,里面的sql语句接受传来的username数据进行数据访问。
语句的id="getAgeByUsername"则是对应mapper接口中的方法,resultType="java.lang.Integer"指定本次数据访问的数据返回类型。
<?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.example.demo.mapper.UserMapper">
<select id="getAgeByUsername" resultType="java.lang.Integer">
select
age
from
t_user
where
username=#{username,jdbcType=VARCHAR}
</select>
</mapper>
在application.yml中添加配置mapper.xml文件的路径:
mybatis:
mapper-locations:
- classpath:mybatis/*.xml
在程序入口类中添加扫描:
@MapperScan("com.example.demo.mapper")//添加该注解后,运行程序时会自动扫描指定路径中的mapper接口实现类。
业务逻辑层
UserService类:
package com.example.demo.service;
public interface UserService {
int getAgeByUsername(String username);
}
UserServiceImpl类:
package com.example.demo.service.impl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public int getAgeByUsername(String username) {
return userMapper.getAgeByUsername(username);
}
}
注:在service接口的实现类中,要加上@Service注解,把实现类交给spring处理。
通过@Autowired注解获得自动注入的userMapper实现类,在重写的方法中进行调用,获得数据。
页面展示层:
UserController类:
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/cn")
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/user/age", method = RequestMethod.GET )
public Integer getAgeOfUser(){
return userService.getAgeByUsername("张文赞");
}
}
运行程序
按照配置的端口和映射URL,页面的路径应该是http://localhost:8080/cn/cn/user/age
使用git上传代码
1.搜索github
2.添加你的git地址
3.连接
4.git add . 上传
5.git staus 查看
6.git commit -m "修改备注"
7.git remote add origin https://github.com/ZhangWenZna/demo.git 创建连接
8.git remote -v 已经有可以查看
9.git push -u origin master
输入账户和密码