spring的入门编程
一、常用的springjar说明
- spring-aop-5.0.2.RELEASE.jar:spring对aop的支持
- spring-aspects-5.0.2.RELEASE.jar:spring对aspects的集成支持aspects就是一个AOP的开源库
- spring-beans-5.0.2.RELEASE.jar:spring对配置文件的对象的解析与创建
- spring-context-5.0.2.RELEASE.jar:spring 对上下文(应用程序的运行环境)的支持包
- spring-context-support-5.0.2.RELEASE.jar:spring上下文集成的一些第三方主库,如定时器
- spring-core-5.0.2.RELEASE.jar:spring 的核心包
- spring-expression-5.0.2.RELEASE.jar:spring 的相关表达式包
- spring-instrument-5.0.2.RELEASE.jar:辅助包【可以不使用】
- spring-instrument-tomcat-5.0.2.RELEASE.jar:辅助包【对tomcat的支持】
- spring-jdbc-5.0.2.RELEASE.jar:spring 对数据访问的支持,类似DbUtil.java JdbcTemplete
- spring-messaging-5.0.2.RELEASE.jar
- spring-jms-5.0.2.RELEASE.jar:spring 对消息中间件的支持,如可以继承MQ (Message Queue)
- spring-orm-5.0.2.RELEASE.jar
- spring-oxm-5.0.2.RELEASE.jar:都是对第三方 ORM 框架的扩展和集成
- spring-tx-5.0.2.RELEASE.jar:spring 对所有ORM 框架的事务支持
- spring-web-5.0.2.RELEASE.jar:spring 对所有的控制层的框架的集成支持如 struts
- spring-webmvc-5.0.2.RELEASE.jar:springmvc
- spring-websocket-5.0.2.RELEASE.jar:spring 对网页聊天的支持
二、准备工作
1. 创建一个WEB项目并导包
2. 导入日志包并创建log4j.properties
导包
创建db.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3. 创建User实体类
package com.lasing.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String address;
private Date birthday;
public User() {
super();
}
public User(Integer id, String name, String address, Date birthday) {
super();
this.id = id;
this.name = name;
this.address = address;
this.birthday = birthday;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + ", birthday=" + birthday + "]";
}
}
4. 创建UserDao
package com.lasing.dao;
import com.lasing.domain.User;
public interface UserDao {
public User queryUserById(Integer id);
}
5. 创建UserDaoImpl
package com.lasing.dao.impl;
import java.util.Date;
import com.lasing.dao.UserDao;
import com.lasing.domain.User;
public class UserDaoImpl implements UserDao{
public User queryUserById(Integer id) {
User user = null;
switch (id) {
case 1:
user = new User(1,"小明","清远",new Date());
break;
case 2:
user = new User(2,"老王","清远",new Date());
break;
case 3:
user = new User(3,"史莱克","清远",new Date());
break;
}
return user;
}
}
6. 创建UserService
package com.lasing.service;
import com.lasing.domain.User;
public interface UserService {
public User queryUserById(Integer id);
}
7. 创建UserServiceImpl
package com.lasing.service.impl;
import com.lasing.dao.UserDao;
import com.lasing.domain.User;
import com.lasing.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setDao(UserDao userDao) {
this.userDao = userDao;
}
public User queryUserById(Integer id) {
return userDao.queryUserById(id);
}
}
8. 创建UserController
package com.lasing.controller;
import com.lasing.domain.User;
import com.lasing.service.UserService;
public class UserController {
private UserService userService;
public void setService(UserService userService) {
this.userService = userService;
}
public void queryUserById() {
Integer id = 1;
User user = userService.queryUserById(id);
System.out.println(user.toString());
}
}
9. 创建并配置applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<!-- 导入头文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- 声明dao -->
<bean id="userDao" class="com.lasing.dao.impl.UserDaoImpl"/>
<!-- 声明service -->
<bean id="userService" class="com.lasing.service.impl.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<!-- 声明controller -->
<bean id="userController" class="com.lasing.controller.UserController">
<property name="userService" ref="userService"/>
</bean>
</beans>
10. 测试
package com.lasing.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lasing.controller.UserController;
public class UserTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println(applicationContext);
UserController userController = (UserController) applicationContext.getBean("userController", UserController.class);
userController.queryUserById();
}
}