IntelliJ 搭建SSM框架

1). 創(chuàng)建項目

按圖片填寫相關(guān)內(nèi)容


圖1.png
2). 填寫相關(guān)配置
圖2.png
3). 配置工具

Web -> web,SQL -> MySQL 和 MyBatis


圖3.png

圖4.png
4). 配置項目
圖5.png
5). application.properties配置
#tomcat端口
server.port=8080
#數(shù)據(jù)連接
# 連接地址
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf8
# 用戶名
spring.datasource.username=root
# 密碼
spring.datasource.password=root
# 驅(qū)動名
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#Mybatis掃描
mybatis.mapper-locations=classpath*:mapper/*.xml
6). 數(shù)據(jù)庫表
圖6.png
7). 創(chuàng)建用戶實體類
  • User
/**
 * 用戶實體類
 */
data class User (
        val id: Long,
        val name: String,
        val age: Int
)
8). 創(chuàng)建UserDao
/**
 * 要為Dao層接口上面添加一個@Mapper注解。
與springbootApplication中的@MapperScan二選一寫上即可
 */
@Mapper
interface UserDao {
    fun selectUserByName(name: String): User
}
9). 創(chuàng)建UserMapper.xml

創(chuàng)建位置:resources/mapper/

  • 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.mazaiting.blog.dao.UserDao">
    <select id="selectUserByName" resultType="com.mazaiting.blog.domain.User">
        SELECT * FROM user WHERE name = #{name}
    </select>
</mapper>
10). 創(chuàng)建UserService
@Service
class UserService {
    @Autowired
    lateinit var userDao: UserDao

    fun selectUserByName(name: String): User {
        return userDao.selectUserByName(name)
    }
}
11). 創(chuàng)建UserController
@Controller
class UserController {
    @Autowired
    lateinit var userService: UserService

    @RequestMapping("/select")
    @ResponseBody
    fun selectUserByName(): User {
        return userService.selectUserByName("mazaiting")
    }
}
12). 部署項目,瀏覽器訪問http://localhost:8080/select
圖7.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容