飞道的博客

从spring boot到spring cloud(三)springmvc+spring+mybatis

451人阅读  评论(0)

本章先用非微服务的方式搭建一个本地springmvc+spring+mybatis,然后再分解成微服务。



选择springmvc+mybatis+mysql驱动。

mybatis的建表和生成对象这里就不说了看一查看以前的

在eclipse中使用MyBatisGenerator生成MyBatis实体类


生成以后产生相应的目录
mapping xml存放目录
entity 数据库bean对象存放目录
dao dao层存放目录

server:
  port: 8082
 
spring:
  datasource:
    username: root
    password: ******
    url: jdbc:mysql://139.*.*.*:3306/zskxt?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC&characterEncoding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      # 下面为连接池的补充设置,应用到上面所有数据源中
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 1000
      # 配置获取连接等待超时的时间
      max-wait: 600000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      max-pool-prepared-statement-per-connection-size: 20
      filters: stat,wall
      use-global-data-source-stat: true
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=50000
      validationQuery: SELECT 'x'
                  
mybatis:
  mapper-locations: classpath:com/zskwebxt/mapping/*.xml
 
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

mapper-locations: classpath:com/zskwebxt/mapping/*.xml 配置成xml存放目录

运行

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ComponentScan(basePackages={
   "com.zskwebxt.*"})  //spring扫描的控制层
@SpringBootApplication
@EnableAsync
@MapperScan("com.zskwebxt.dao") //dao层存放空间
public class ZskwebxtApplication implements WebMvcConfigurer{
   

	public static void main(String[] args) {
   
		SpringApplication.run(ZskwebxtApplication.class, args);
	}
	
	//跨域	
	@Override
    public void addCorsMappings(CorsRegistry registry) {
   
        registry.addMapping("/**")
        .allowedOrigins("*")
        .allowedMethods("*") //.allowedMethods("PUT", "DELETE","POST","GET")
        .allowedHeaders("*").maxAge(3600);
    }
}

控制层代码

@RestController
@RequestMapping("/getss")
public class ssController {
   

	@Autowired
	private zsk_book_smMapper zsk_book_smMapper;  //注入dao层对象

	@RequestMapping("/sousuo")  //请求名称
	@ResponseBody
	public Returnben sousuo(HttpServletRequest request, HttpSession session,@RequestBody Map<String, Object> json) {
   
		Returnben returnben = new Returnben();
		String bookname = json.get("bookname").toString();
		List<zsk_book_sm> m=zsk_book_smMapper.selectbysousuo(bookname);
		returnben.setObj(m);
		returnben.setMsg("成功");
		returnben.setSuccess("0");
		return returnben;
	}
}

ajax请求
需要引用

<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
var data = {
		bookname: $("#bookname").val()
	}

	console.log(data)
	var tj = JSON.stringify(data);
	$.ajax({
		url: "http://127.0.0.1:8082/getss/sousuo",
		data: tj,
		type: "POST",
		dataType: "json",
		contentType: "application/json",
		success: function(data) {
			if (data.success == 0) {
				var html = "";
				$.each(data.obj, function(i, val) {
					html +=
						'<li class="searchItem" data-link="/html/'+val.bookid+'.html"> <div class="book"> <a class="cover" href="/html/'+val.bookid+'.html" target="_blank"> <img width="114" height="160" src="http://139.186.70.122:8081/'+val.tp+'"> <span class="png lian"></span> </a> <h5><a href="/html/'+val.bookid+'.html" target="_blank">'+val.bookname+'</a></h5> <p class="author">作者:'+val.zz+'</p> <div class="w-star w-star1"> <span>&nbsp;</span> <span>&nbsp;</span> <span>&nbsp;</span> <span class="no">&nbsp;</span> <span class="no">&nbsp;</span> </div> <p class="price">  </p> <p class="disc"><span class="j-cutted" words="72">'+val.msnr+'</span></p> </div> </li>'
				})
				$("#J_Container").html(html);
				$("#xianshi").html('“'+$("#bookname").val()+'”的搜索结果');
			}
		}
	})

以上就是简单的 springmvc+spring+mybatis 有什么问题可以私信,后续建立分布式和集群微服务。


转载:https://blog.csdn.net/terry711/article/details/108170317
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场