今天抽空学习快速搭建第一个SSM框架,需要的所有jar包在以下百度云链接中请自行下载。
链接:https://pan.baidu.com/s/18IDmHvByrrGTXBu023U15Q 密码:wufn
一、创建Dynamic Web项目,并导入jar包
创建一个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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<select id="findAgeById" parameterType="String" resultType="int">
select age from admin WHERE id=#{id}
</select>
<select id = "findNameById" parameterType="String" resultType="String">
select username from admin where id=#{id}
</select>
</mapper>
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);
}
U