整合MyBatis - 实现基于mybatis的CRUD功能

杜辰龙
2023-12-01

请结合springboot学习教程项目github地址 https://github.com/heng1234/spring-boot_one来理解

 

pom文件:

<?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>

	<groupId>com.yh</groupId>
	<artifactId>springdemoOne</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springdemoOne</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
	
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
	
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
		</dependency>

		<!--mybatis-->
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.3.1</version>
		</dependency>
		
		

		
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

配置文件:

############################################################
#
# 配置数据源相关	使用阿里巴巴的 druid 数据源
#
############################################################
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true


############################################################
#
# mybatis 配置
#
############################################################
# mybatis 配置
#mybatis
#存放mapper xml位置
mybatis.mapper-locations=classpath:mapper/*.xml

在springboot启动类加上扫描  

package com.yh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.yh.mapper")
@ComponentScan(basePackages= {"com.yh","org.n3r.idworker"})

@EnableAsync
public class SpringdemoOneApplication {

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

建立TuserService 接口

package com.yh.service;

import java.util.List;

import com.yh.pojo.TUser;

public interface TuserService {
	/**
	 * 增加
	 * <p>Title: saveTuser</p>  
	 * <p>Description: </p>  
	 * @param tUser
	 */
	public void  saveTuser(TUser tUser);
		
	/**
	 * 修改
	 * <p>Title: updateTuser</p>  
	 * <p>Description: </p>  
	 * @param tUser
	 */
	public void updateTuser(TUser tUser);
	
	/**
	 * 根据id查询
	 * <p>Title: findTuser</p>  
	 * <p>Description: </p>  
	 * @param id
	 * @return
	 */
	public TUser findTuser(Integer id);
	
	
	/**
	 * 查询全部
	 * <p>Title: findTusers</p>  
	 * <p>Description: </p>  
	 * @return
	 */
	public List<TUser> findTusers();
		
	/**
	 * 分页查询
	 * <p>Title: findPageListTuser</p>  
	 * <p>Description: </p>  
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<TUser> findPageListTuser(int page,int pageSize);
	
	

}

TuserService 接口实现类

package com.yh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.yh.mapper.TUserMapper;
import com.yh.pojo.TUser;
import com.yh.service.TuserService;

import tk.mybatis.mapper.entity.Example;

@Service("tuserService")
public class TuserServiceImpl implements TuserService {
	
	@Autowired
	private TUserMapper tuserMapper;
	
	/**
	 * 增加
	 * <p>Title: saveTuser</p>  
	 * <p>Description: </p>  
	 * @param tUser
	 */
	@Override
	
	public void saveTuser(TUser tUser) {
		tuserMapper.insertSelective(tUser);
		int num = 1 / 0;
		TUser tUser2 = new TUser();
		tUser2.setUserid(1009);
		tUser2.setPassword("123456");
		tuserMapper.updateByPrimaryKeySelective(tUser2);
		

	}
	/**
	 * 修改
	 * <p>Title: updateTuser</p>  
	 * <p>Description: </p>  
	 * @param tUser
	 */
	@Override

	public void updateTuser(TUser tUser) {
		tuserMapper.updateByPrimaryKeySelective(tUser);

	}
	/**
	 * 根据id查询
	 * <p>Title: findTuser</p>  
	 * <p>Description: </p>  
	 * @param id
	 * @return
	 */
	@Override

	public TUser findTuser(Integer id) {
		
		return tuserMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询全部
	 * <p>Title: findTusers</p>  
	 * <p>Description: </p>  
	 * @return
	 */
	@Override
	
	public List<TUser> findTusers() {
		// TODO Auto-generated method stub
		return tuserMapper.selectAll();
	}
	

}

Controller

   	
	@Autowired
	private TuserService tuserService;
	
	
   /**
	 * 测试增加
	 * <p>Title: saveTuser</p>  
	 * <p>Description: </p>
	 */
	@RequestMapping("saveTuser")
	@ResponseBody
	public String saveTuser() {
		System.out.println(sid.nextShort()+"------this sid"); 
		TUser tUser = new TUser( "heng", "666666", "1111");
		tuserService.saveTuser(tUser);
		
		return "true";
	}

 

 类似资料: