今天抽空学习快速搭建第一个SSM框架,需要的所有jar包在以下百度云链接中请自行下载。
链接:https://pan.baidu.com/s/18IDmHvByrrGTXBu023U15Q 密码:wufn
创建一个Dynamic Web Project并命名为SSM,先看一下该项目的整个框架结构:
先将下载好的jar包全部粘贴到WebContent下WEB-INF里的lib文件夹中,然后开始配置相关文件。
com.model(与数据库表中的成员以及属性一一对应)
新建一个java文件,用来存储数据库中对应的admin属性:
package com.model;
public class User {
private String id = null;
private String username = null;
private String password = null;
private int age = 0;
private String sex = null;
public String getId() {
return id;
}
public void setId(String 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;
}
}
com.mapper(Mabatis逆向工程根据数据库表生成model属性以及mapper.xml文件)
UserMapper.java
package com.mapper;
import java.util.List;
import com.model.*;
public interface UserMapper {
public int findAgeById(String id);
public String findNameById(String id);
}
UserMapper.xml
com.service(业务层,调用UserMapper接口对数据库进行操作)
UserService.java
package com.service;
import java.util.List;
import com.model.*;
public interface UserService {
public int findAgeById(String id);
public String findNameById(String id);
}
UserServiceImpl.java
package com.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mapper.UserMapper;
import com.model.User;
import com.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Resource
public UserMapper userMapper;
@Override
public int findAgeById(String id) {
// TODO Auto-generated method stub
int age = userMapper.findAgeById(id);
return age;
}
@Override
public String findNameById(String id) {
// TODO Auto-generated method stub
String username = userMapper.findNameById(id);
return username;
}
}
com.controller(控制器统一管理)
package com.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.User;
import com.service.UserService;
@Controller
public class UserCotroller {
@Autowired
private UserService userService;
@RequestMapping(value="/hello.do")
public String findAgeById(HttpServletRequest request)
{
int age=userService.findAgeById("1410313114");
System.out.println(age);//如果实验成功,在控制台会打印年龄21
return "index";
}
@RequestMapping(value="/hello1.do")
public String findNameById(HttpServletRequest request)
{
String id = request.getParameter("id");
String username = userService.findNameById(id);
System.out.println(username);
return "index";
}
}
config包(相关配置文件)
1.applicationContext.xml
注:如果想通过数据库连接池对数据库进行操作,除了将上述数据源相关代码换成以下代码,还应在lib中添加相应的druid jar包:
2.mybatis-config.xml
3.spring-mvc.xml
web.xml
SSM
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
hello.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath*:config/applicationContext.xml
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:config/spring-mvc.xml
1
springMVC
*.do
org.springframework.web.util.IntrospectorCleanupListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
到这里,所有的配置文件均已完成。
写一个简单的hello.jsp页面,用来提交一个简单的表单:
写一个index.jsp页面用于提交后的跳转,若成功跳转,则显示ok。
通过测试,在input中输入数据库中admin表的任一行中的id属性,点击提交,则成功跳转到index.jsp页面,并在控制台成功输出该id属性对应的username。
在以后的时间里,会慢慢深入了解框架的底层原理,并及时总结与分享。