一、SSM開發實戰教程
SSM是一種使用Spring、Spring MVC和MyBatis框架組合進行開發的Java Web應用程序架構。下面為大家介紹SSM開發實戰教程。
1.搭建基礎框架
首先要新建一個maven項目,並在pom.xml文件中添加相關依賴。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
2.建立數據庫
建立相關數據庫表,可以使用MySQL或其他數據庫。
3.創建實體類
新建JavaBean實體類,並使用註解進行相關配置,如下所示:
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private String password;
}
4.創建Mapper接口
創建Mapper接口,可以使用MyBatis Generator工具快速生成,也可以手動編寫。使用註解配置查詢方法,如下所示:
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User selectByPrimaryKey(Integer id);
@Insert("INSERT INTO user(name, password) VALUES(#{name}, #{password})")
int insert(User record);
@Delete("DELETE FROM user WHERE id = #{id}")
int deleteByPrimaryKey(Integer id);
@Update("UPDATE user SET name=#{name}, password=#{password} WHERE id = #{id}")
int updateByPrimaryKey(User record);
}
5.創建Service接口和實現類
創建Service接口和實現類,使用@Autowired註解注入Mapper,並編寫相關業務邏輯,如下所示:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
public int insert(User record) {
return userMapper.insert(record);
}
public int deleteByPrimaryKey(Integer id) {
return userMapper.deleteByPrimaryKey(id);
}
public int updateByPrimaryKey(User record) {
return userMapper.updateByPrimaryKey(record);
}
}
6.創建Controller層
創建Controller類,添加@RequestMapping註解進行相關路徑映射,如下所示:
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/select/{id}")
public User selectByPrimaryKey(@PathVariable("id") Integer id) {
return userService.selectByPrimaryKey(id);
}
@RequestMapping("/insert")
public int insert(User user) {
return userService.insert(user);
}
@RequestMapping("/delete/{id}")
public int deleteByPrimaryKey(@PathVariable("id") Integer id) {
return userService.deleteByPrimaryKey(id);
}
@RequestMapping("/update")
public int updateByPrimaryKey(User user) {
return userService.updateByPrimaryKey(user);
}
}
二、SSM開發實戰項目源碼
以下為SSM開發實戰項目的完整源碼,供大家參考。
1.項目結構
項目整體結構如下圖所示:
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ ├── config
│ │ │ ├── DataSourceConfig.java
│ │ │ └── MyBatisConfig.java
│ │ ├── controller
│ │ │ └── UserController.java
│ │ ├── mapper
│ │ │ └── UserMapper.java
│ │ ├── model
│ │ │ └── User.java
│ │ ├── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ ├── Application.java
│ │ └── MvcConfig.java
│ └── resources
│ ├── mapper
│ │ └── UserMapper.xml
│ ├── static
│ └── application.properties
└── test
├── java
└── resources
2.源碼下載
源碼下載地址:https://github.com/horaceqz/SSM-demo
三、SSM管理實戰類項目
下面為大家推薦幾個SSM管理實戰類的項目,可以供大家參考。
1.物料管理系統
物料管理系統主要用於企業的物料管理流程。管理員可以管理物料、對庫存進行統計等。
2.人事管理系統
人事管理系統主要用於企業的人事管理流程。包括員工管理、僱傭管理、招聘管理等。
3.圖書管理系統
圖書管理系統主要用於圖書館管理流程。管理員可以管理書籍、讀者、借閱記錄等。
以上是SSM項目實戰完整教程的詳細闡述,相信對大家學習和使用SSM框架有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/248424.html