SpringBoot ssm 框架demo

框架:ssm :Spring 、SpringMVC、Mybatis

1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.4.2</version>
				<configuration>
				  <skipTests>true</skipTests>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
application.yml配置文件(jdbcUrl和password填写自己的数据库连接)
db:
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl:xxxx
    username: root
    password: xxxx
    connectionTimeout: 30000
    idleTimeout: 3600000
    maxLifetime: 14400000
    maximumPoolSize: 60
    minimumIdle: 5
    isAutoCommit: false
    readOnly: false
    
mybatis:
  mapper-locations: classpath*:mapper/*.xml
Application入口类
@SpringBootApplication
@MapperScan(basePackages={"com.example.demo.dao"})

public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
model:实体层
public class UserMessage implements Serializable {
    private static final long serialVersionUID = 1L;
		private Integer id;
		
    	private Integer userId;
    	
		 public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
        
}
DAO层:dao层是接口,并且没有实现类。在mapper.xml中实现
@Repository
@MapperScan
public interface UserMessageDao {

/**
 * 添加记录
 *
 * @param userMessage userMessage
 * @return PrimaryKey
 */
public Integer addUserMessage(UserMessage userMessage);


/**
 * 根据ID获取记录
 *
 * @param id 主键ID
 * @return UserMessage
 */
public UserMessage findUserMessage(Integer id);

/**
 * 根据ID删除记录
 *
 * @param id 主键ID
 * @return 删除记录的条数
 */
public Integer deleteUserMessage(Integer id);

/**
 * 更新所有字段
 *
 * @param userMessage userMessage
 * @return 更新记录的条数
 */
public Integer updateUserMessage(UserMessage userMessage);
/**
 * 获取满足条件的记录,带分页功能
 *
 * @param dto 查询条件
 * @param startNo 开始行
 * @param pageSize 每页记录数
 * @return 满足条件的记录
 */
public List<UserMessage> findUserMessageWithPg(UserMessageDto dto, Long startNo, Integer pageSize);
}
service 层和controller层直接引用下层就可以了,这里省略。
mapper.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">
<!-- * ***************************** * creat by wanfeizhang * Wed Jan 13 
	11:11:00 CST 2016 * ***************************** -->
<mapper namespace="com.example.demo.dao.UserMessageDao">
mapper.xml
<mapper namespace="com.example.demo.dao.UserMessageDao">

<!-- 默认查询结果映射 -->
<resultMap id="UserMessageResult" type="com.example.demo.model.UserMessage">
	<result column="id" property="id" />
	<result column="user_id" property="userId" />
	<result column="msg_type" property="msgType" />
	<result column="sub_type" property="subType" />
	<result column="content" property="content" />
	<result column="msg_status" property="msgStatus" />
	<result column="read_time" property="readTime" />
	<result column="create_time" property="createTime" />
	<result column="update_time" property="updateTime" />
</resultMap>

<!-- 按userId,msgType分组统计结果映射 -->
<resultMap id="countGroupByUserIdMsgTypeResult" type="com.example.demo.model.dto.StatUserMessageVO">
	<result column="user_id" property="userId" />
	<result column="msg_type" property="msgType" />
	<result column="msg_count" property="msgCount" />
</resultMap>

<!-- 保存方法 -->
<insert id="addUserMessage" >
	insert into user_message (
	user_id,msg_type,sub_type,`content`,msg_status,read_time,create_time,update_time) values (
	#{userId}, #{msgType}, #{subType}, #{content}, #{msgStatus},#{readTime}, now(),now() )
</insert>

<!-- 根据ID查询记录 -->
<select id="findUserMessage" resultMap="UserMessageResult">
	select
	t.id,t.user_id,t.msg_type,t.sub_type,t.content,t.msg_status,t.read_time,t.create_time,t.update_time
	from user_message t where t.id=#{id}
</select>

<!-- 根据ID删除记录 -->
<delete id="deleteUserMessage">
	delete from
	user_message where id=#{id}
</delete>

<!-- 更新所有字段信息 -->
<update id="updateUserMessage">
	update
	user_message t set
	t.user_id=#{userId},t.msg_type=#{msgType},t.sub_type=#{subType},t.content=#{content}
	,t.msg_status=#{msgStatus},t.read_time=#{readTime},t.update_time=now()
	where id=#{id}
</update>
</mapper>
测试:
import com.example.demo.DemoApplication;
import com.example.demo.model.UserMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserMessageServiceTest {


	@Resource
	private UserMessageService userMessageService;



	@Test
	public void batchAddUserMessage() throws Exception {
		List<UserMessage> list=new ArrayList();
		for (int i=0;i<2;i++){
			UserMessage userMessage=new UserMessage();
			userMessage.setUserId(15);
			userMessage.setMsgType(2);
			userMessage.setMsgSubType(UserMessage.MsgSubType.BALANCE);
			userMessage.setContent("批量插入数据测试");
			userMessage.setMsgStatus(1);
			list.add(userMessage);
		}
		Integer integer = userMessageService.batchAddUserMessage(list);
		System.out.println("数据库插入的条数是:"+integer);
	}

	@Test
	public void findUserMessage() throws Exception {
		UserMessage userMessage = userMessageService.findUserMessage(493803);
		System.out.println("id为493803的用户信息是:"+userMessage);
	}

	@Test
	public void deleteUserMessage() throws Exception {
		Integer integer = userMessageService.deleteUserMessage(493809);
		System.out.println("删除用户信息受影响的行数是:"+integer);
	}

	@Test
	public void updateUserMessage() throws Exception {
		UserMessage userMessage=new UserMessage();
		userMessage.setId(493804);
		userMessage.setUserId(15);
		userMessage.setMsgType(2);
		userMessage.setMsgSubType(UserMessage.MsgSubType.BALANCE);
		userMessage.setContent("更新测试");
		userMessage.setMsgStatus(1);
		Integer integer = userMessageService.updateUserMessage(userMessage);
		System.out.println("493804更新用户消息受影响的行数是:"+integer);
	}

控制台可以正常打印用户信息,数据库连接,service层,dao层,mapper.xml之间可以正常引用

在这里插入图片描述

ssm框架,基础的应用框架。解释不多,有疑问欢迎留言!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值