一,spring工程搭建(引入四个包):spring-core、spring-beans、spring-context、spring-expression
二,常用的后端代码结构:Controller、Service、Dao
方法的调用流程:数据库通过SQL传递给Dao、Dao层通过entity到Service、Service层通过vo到Controller层
三,if判断和三目运算符:if是判断语句 如果结果为真会执行代码1,如果是假执行代码2
三目运算符:对比与if更为简单如果if语句中代码只有一行语句时会用三目运算符来替代if语句(如果语句比较简单用三目运算符比if更好)
四,Spring IOC & DI(spring的控制反转与依赖注入):User.java、UserVo.java、UserDao.java、UserDaoImpl.java、UserService.java、UserServiceImpl.java、UserController.java、UserTest.java
if判断和三目运算符
public class Test {
public static Integer getMaxIf(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
public static Integer getMaxTernary(int a, int b) {
return a > b ? a : b;
}
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println("if 返回最大值" + getMaxIf(a, b));
System.out.println("三目运算符 返回最大值" + getMaxTernary(a, b));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
https://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<!-- bean definitions here -->
<context:component-scan base-package="dao"/>
<context:component-scan base-package="services"/>
<context:component-scan base-package="controller"/>
</beans>
package services.impl;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;
public UserVo getVo(Integer id) {
User user = userDao.getEntity(id);
UserVo userVo = new UserVo(user);
userVo.setGenderName(userVo.getGender() == 0 ? "female" : "male");
return userVo;
}
}