zoukankan      html  css  js  c++  java
  • springboot整合mybatis

    springboot整合mybatis

    〇、搭建springbboot基础环境

    一、添加依赖

    <!-- mybatis -->
    <dependency> <!-- 最好用最新版本 -->
    	<groupId>org.mybatis.spring.boot</groupId>
    	<artifactId>mybatis-spring-boot-starter</artifactId>
    	<version>1.3.0</version>
    </dependency>
    

    二、配置文件(application.properties)

    spring.mvc.view.prefix=/WEB-INF/
    spring.mvc.view.suffix=.jsp
    
    ########################################################
    ###datasource
    ########################################################
    spring.datasource.url = jdbc:mysql://localhost:3306/mine
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    spring.datasource.max-active=20
    spring.datasource.max-idle=8
    spring.datasource.min-idle=8
    spring.datasource.initial-size=10
    

    二、pojo

    import java.math.BigDecimal;
    import java.util.Date;
    
    public class Item {
    	private int iid;
    	private String iname;
    	private BigDecimal icount;
    	private String iremarks;
    	private String categortys;
    	private Date itime;
    	
    	
    	public Date getItime() {
    		return itime;
    	}
    	public void setItime(Date itime) {
    		this.itime = itime;
    	}
    	public String getIname() {
    		return iname;
    	}
    	public void setIname(String iname) {
    		this.iname = iname;
    	}
    	public BigDecimal getIcount() {
    		return icount;
    	}
    	public void setIcount(BigDecimal icount) {
    		this.icount = icount;
    	}
    	public String getIremarks() {
    		return iremarks;
    	}
    	public void setIremarks(String iremarks) {
    		this.iremarks = iremarks;
    	}
    	public String getCategortys() {
    		return categortys;
    	}
    	public void setCategortys(String categortys) {
    		this.categortys = categortys;
    	}
    	public int getIid() {
    		return iid;
    	}
    	public void setIid(int iid) {
    		this.iid = iid;
    	}
    	@Override
    	public String toString() {
    		return "Item [iid=" + iid + ", iname=" + iname + ", icount=" + icount + ", iremarks=" + iremarks
    				+ ", categortys=" + categortys + "]";
    	}
    }
    

    三、Mapper

    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    
    import com.xujie.pojo.Item;
    
    @Mapper  //这个注解帮助生成mapper的一个实现类
    public interface CountDao {
    	
    	@Insert("INSERT INTO `mine`.`item` ( `iname`, `itime`, `icount`, `iremarks`) VALUES ( #{iname}, #{itime}, #{icount}, #{iremarks});")
    	void insert(Item item);
    }
    

    三、Service&&ServiceImpl

    ServiceImpl

    import com.xujie.pojo.Item;
    public interface CountService {
    	void insert(Item item);
    }  
    

    Service

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.xujie.mapper.CountDao;
    import com.xujie.pojo.Item;
    import com.xujie.service.CountService;
    
    @Service
    public class CountServiceImpl implements CountService {
    	
    	@Autowired
    	private CountDao countDao;
    
    	@Override
    	public void insert(Item item) {
    		this.countDao.insert(item);
    	}
    }
    

    三、Controller

    import java.util.Date;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import com.xujie.pojo.Item;
    import com.xujie.service.CountService;
    
    @Controller
    public class CountController {
    	
    	@Autowired
    	private CountService countService;		
    	
    	//添加
    	@PostMapping(value="/test.action")
    	public String test2(Item item) {       //这里接收一个item对象 
    		item.setItime(new Date());
    		this.countService.insert(item);
    		return "index";    //返回到页面
    	}
    }
    

    注意:省略了页面(index.jsp)的编码

  • 相关阅读:
    面向对象并不是必要的
    linq 总结
    垃圾自动回收的一个方案
    随手记 手机软件的不足,和开发自己理财软件的想法
    以人的角度去解决问题
    浮点数比较
    集中原则——软件设计之道
    云在何方
    我遇到了DLL地狱
    在C#.net中如何操作XML
  • 原文地址:https://www.cnblogs.com/xujie09/p/8462192.html
Copyright © 2011-2022 走看看