![timg](https://i-blog.csdnimg.cn/blog_migrate/380af309fd43ea44fc1b489d3d2bde2f.png)
软件工程小学期第一天,学习内容包括:复习JAVA基础知识、熟悉主流框架,总结如下:
1. 输入一组数,计算平均值
程序代码:
import java.util.Scanner;
public class Average {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int s = 0; // 所有数之和
int n = 0; // 数据个数
System.out.println("输入数据个数");
n = scanner.nextInt();
for (int i = 0; i < n; i++) {
s += scanner.nextInt();
}
System.out.println(s / n);
}
}
执行结果:
![实训1-1](https://i-blog.csdnimg.cn/blog_migrate/441172cf07991a82c241c5b01d15e119.png)
2. 定义一个集合类,实现add()、set()、size()、get()方法,要求集合能够自动扩容
实现set()、size()、get()方法较为简单。
本题关键在于如何实现集合类的自动扩容,因为JAVA数组不支持动态修改长度,于是经思考后得出一般思路:集合已满后再添加数据时,创建一个比原来数组更长的数组,并把之前的数据拷贝进新数组,来代替原先长度较小的数组,来实现集合类的自动扩容。
程序代码:
package demo;
public class MySet {
private Object[] list; // 内置数组
private int length = 0; // 集合中元素个数
public MySet(int mSize) {
list = new Object[mSize];
}
// 添加元素
public void add(Object object) {
if(length == list.length) {
Object[] list2 = new Object[2 * list.length];
for(int i = 0;i < length;i++)
list2[i] = list[i];
list = list2;
}
list[length] = object;
length++;
}
// 获取下标为i的元素
public Object get(int i) {
if (i < 0 || i >= length) {
throw new IllegalArgumentException("获取失败,参数不能负数以及不能大于当前数组元数个数");
}
return list[i];
}
// 将下标为i的元素置为obj
public void set(int i,Object obj) {
if (i < 0 || i >= length) {
throw new IllegalArgumentException("设置失败,设置位置不能负数以及不能大于当前数组元数个数");
}
list[i] = obj;
}
// 获取集合大小
public int size() {
return length;
}
// 打印
public static void show(MySet myset) {
for (int i = 0;i < myset.size();i++) {
System.out.println("set.get(" + i + "): " + (String)myset.get(i));
}
}
public static void main(String[] args) {
MySet set = new MySet(4);
set.add("hh");
set.add("ee");
set.add("ll");
set.add("oo");
set.add("ww");
set.set(0, "HH");
show(set);
System.out.println("set.size(): " + set.size());
}
}
执行结果:
![QQ截图20190901090451](http://www.tunan.work:8090/upload/2019/9/QQ%E6%88%AA%E5%9B%BE20190901090451-0964f3e9110140aba54dd4173b5784d9.png)
3. 实现一个实体的增删改查
为复习框架知识,本程序使用SpringBoot框架实现
- 实体层
package com.imooc.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Min;
import java.io.Serializable;
@Entity
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
public class Girl {
@Id
@GeneratedValue
private Integer id;
private String cS;
@Min(value = 18,message = "未成年少女禁止入内")
private Integer age;
public Girl() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCS() {
return cS;
}
public void setCS(String cS) {
this.cS = cS;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- Service层
package com.imooc.service;
import com.imooc.domain.Girl;
import com.imooc.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class GirlService {
@Autowired
private GirlRepository girlRepository;
@Transactional
public void insertTwo(){
Girl girlA = new Girl();
girlA.setCS("A");
girlA.setAge(18);
girlRepository.save(girlA);
Girl girlB = new Girl();
girlB.setCS("BBBB");
girlB.setAge(19);
girlRepository.save(girlB);
}
}
- repository
package com.imooc.repository;
import com.imooc.domain.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GirlRepository extends JpaRepository<Girl,Integer> {
public List<Girl> findByAge(Integer age);
}
- Controller层
package com.imooc.controller;
import com.imooc.domain.Girl;
import com.imooc.repository.GirlRepository;
import com.imooc.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
public class GirlController {
@Autowired
private GirlRepository girlRepository;
@Autowired
private GirlService girlService;
//查询所有女生列表
@GetMapping(value = "/girls")
public List<Girl> girlList(){
return girlRepository.findAll();
}
//添加一个女生
@PostMapping(value = "/addGirl")
public Girl girlAdd(@Valid Girl girl, BindingResult bindingResult){
if (bindingResult.hasErrors()){
System.out.println(bindingResult.getFieldError().getDefaultMessage());
return null;
}
girl.setCS(girl.getCS());
girl.setAge(girl.getAge());
return girlRepository.save(girl);
}
@GetMapping(value = "/girls/{id}")
public Girl getGirl(@PathVariable("id") Integer id){
return girlRepository.getOne(id);
}
@GetMapping(value = "/updateGirl/{id}")
public Girl girlUpdate(@PathVariable("id") Integer id,
@RequestParam("cS") String cS,
@RequestParam("age") Integer age){
System.out.println("ss");
Girl girl = new Girl();
girl.setId(id);
girl.setAge(age);
girl.setCS(cS);
Girl girl1 = girlRepository.save(girl);
System.out.println(girl.getCS());
System.out.println(girl.getAge());
return girl1;
}
@GetMapping(value = "/delGirl/{id}")
public void delGirl(@PathVariable("id") Integer id){
girlRepository.delete(id);
}
@GetMapping(value = "/findByAge/{age}")
public List<Girl> findByAge(@PathVariable("age") Integer age){
return girlRepository.findByAge(age);
}
@PostMapping(value = "/girls/two")
public void girlTwo(){
girlService.insertTwo();
}
}
- Properties
package com.imooc.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlPropertites {
private String cS;
private Integer age;
public String getCS() {
return cS;
}
public void setCS(String cS) {
this.cS = cS;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- 配置文件
# application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
jackson:
serialization:
FAIL_ON_EMPTY_BEANS:
false
今天的讲课中,再次听到一句老生常谈的话题,即JDBC模式已经过时,当今用最多的仍是三大框架,而Springboot正在流行。