Spring+SpringMVC+Mybatis整合开发(超级详细)提⽰:⽂章写完后,⽬录可以⾃动⽣成,如何⽣成可参考右边的帮助⽂档
⽂章⽬录
⼀、搭建基础⼯程
1. 创建⼀个空的maven⼯程
2. 添加pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ssm_mytest</groupId>
<artifactId>ssm_mytest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<mavenpiler.source>8</mavenpiler.source>
<mavenpiler.target>8</mavenpiler.target>
</properties>
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId&hange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
读取配置文件失败<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependency>
<!--Mybatis-->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>产假多少天2020新规定
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
</dependency>
</dependencies>
<!-- Maven静态资源导出-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
冰期时代</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
3. 添加WEB框架⽀持
4. 补全项⽬结构
4.1补全项⽬包结构
4.2添加Spring的配置⽂件l(⽤于最后整合所以的配置信息,暂且空置)
最终的项⽬结构如下:
⼆、Mybatis层编写以及数据库连接
1. 开始编写pojo类
package;
import AllArgsConstructor;
import Data;
import NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
int userId;
String userName;
int age;
String sex;
}
2. 编写Dao层Mapper接⼝
import List;
public interface UserMapper {
List<User>getAllUser();
User getUserById(@Param("userId")int userId);
int addUser(User users);
int deleteUser(@Param("userId")int userId);
int updateUser(User user);
}
3. 编写对应的l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC"-////DTD Config 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.leshangju.dao.UserMapper">
<insert id="addUser" parameterType="User">
insert into users(userName, age, sex)
values(#{userName}, #{age}, #{sex})
</insert>
<delete id="deleteUser" parameterType="int">
delete
from users
where userId =#{userId};
</delete>
<update id="updateUser" parameterType="User">
update users
set userName=#{userName},
age=#{age},何韵诗吧
sex=#{sex}
where userId =#{userId}
</update>
<select id="getUserById" parameterType="int" resultType="User"> select*
from users
where userId =#{userId};
</select>
<select id="getAllUser" resultType="User">
select*
from users;
</select>
</mapper>
4. 编写service接⼝
import List;
public interface UserService {
List<User>getAllUser();
User getUserById(@Param("userId")int userId);
int addUser(User users);
int deleteUser(@Param("userId")int userId);
int updateUser(User user);
}
5. 编写impl实现类
package;
import UserMapper;
import User;
import Autowired; import List;
public class UserServiceImpl implements UserService {
UserMapper userMapper;
public void setUserMapper(UserMapper userMapper){
this.userMapper = userMapper;
}
@Override
public List<User>getAllUser(){
王子文前男友AllUser();
}
@Override
public User getUserById(int userId){
UserById(userId);
}
@Override
public int addUser(User users){
return userMapper.addUser(users);
}
@Override
public int deleteUser(int userId){
return userMapper.deleteUser(userId);
}
5.21是什么日子@Override
public int updateUser(User user){
return userMapper.updateUser(user);
}
}
发布评论