这段时间一直在观看Spring框架,自己去查询资料手动的去配置Spring文件,和Jdbc连接数据库进行crud的操作:
使用Maven加载项目中所需要的依赖jar:
web.xml配置:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
spring-mvc.xml:
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
使用:
@Component
public class UserDaoImpl implements UserDao{
@Autowired
JdbcTemplate jdbcTemplateObject;
/**
* 添加用户信息:
*/
public void createUser(User user) {
System.err.println(user.getName()+"---"+user.getPassword()+"----jdbcTemplateObject"+jdbcTemplateObject);
String SQL = "insert into test (name, password) values (?, ?)";
String sql= "INSERT INTO test(name,password) VALUES(?,?)";
int update = jdbcTemplateObject.update(SQL,user.getName(),user.getPassword());
if(update<=0) {
System.out.println("添加失败");
}else {
System.out.println("添加成功");
}
}
/**
* 查询用户信息:
*/
public User findOne(int id) {
String SQL = "select * from test where id = ?";
User student = jdbcTemplateObject.queryForObject(SQL,
new Object[]{id}, new StudentMapper());
return student;
}
spring mvc配置所有的配置信息都写过注释,这所有的配置以及测试并且运行过,
在配置过程中发现,spring的强大,spring bean的自动装配使得spring更加的方便和好用
也就是IOC和Dl的强大之处