springboot mybatis 整合
阿新 • • 發佈:2018-08-22
char override ssp figure private on() new doctype xml文件
新建項目在上一篇.
第二步:創建表和相應的實體類
實體類:user.java
package com.qtt.im.entity; import java.io.Serializable; public class User implements Serializable { private Long id; private String loginName; private String pass; private String mobile; private String email; private String name;public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getPass() {return pass; } public void setPass(String pass) { this.pass = pass; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getEmail() { return email; } publicvoid setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
第三步:創建寫mapper接口,Service,controller
以一個查詢為例:
UserMapper.java(此接口用於xml文件和service之間的連接)
package com.qtt.im.mapper; import com.qtt.im.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { public User getUserList(); }
UserService.java
package com.qtt.im.service; import com.qtt.im.entity.User; public interface UserService { public User getUserList(); }
UserServiceImpl.java(usermapper會報錯,但是不影響運行)
package com.qtt.im.service.impl; import com.qtt.im.entity.User; import com.qtt.im.mapper.UserMapper; import com.qtt.im.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper usermapper; public User getUserList() { return usermapper.getUserList(); } }
UserController.java
package com.qtt.im.controller; import com.qtt.im.entity.User; import com.qtt.im.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUserList") public User getUserList(){ User userList = userService.getUserList(); return userList; } }
所用的註解可以自己網上查詢.
第四步:可以添加一些過濾器
MyFilter.java
package com.qtt.im.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; System.out.println("this is my filter url:"+request.getRequestURI()); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
WebConfiguration.java
package com.qtt.im.filter; import org.apache.catalina.filters.RemoteIpFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter(){ return new RemoteIpFilter(); } @Bean public FilterRegistrationBean testFilterRegistration(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new MyFilter()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.addInitParameter("paramName","paramValue"); filterRegistrationBean.setName("MyFilter"); filterRegistrationBean.setOrder(1); return filterRegistrationBean; } }
第五步:編寫一個程序入口
Application.java
package com.qtt.im; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.qtt.im.mapper") public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
java代碼部分的結構圖如下:
第六步:創建寫sql語句的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"> <mapper namespace="com.qtt.im.mapper.UserMapper" > <select id="getUserList" resultType="com.qtt.im.entity.User" > SELECT id,loginName FROM im_user; </select> </mapper>
其中 namespace:是java部分的userMapper接口.resultType:是實體類.返回類型.
第七步:創建配置文件
application.proterties
spring.datasource.url=jdbc:mysql://localhost:3306/sdt?useUnicode=true&characterEncoding=utf-8 (sdt是數據庫) mybatis.type-aliases-package=com.qtt.im.mapper (mapper接口掃描) spring.datasource.driver-class-name = com.mysql.jdbc.Driver (驅動) spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.username=root (用戶名) spring.datasource.password=qintongtong (密碼) mybatis.mapper-locations=classpath*:/mappers/**Mapper.xml (xml文件)
log4j.properties
log4j.rootLogger=WARN, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n
配置文件的結構圖如下:
註:userMapper.xml文件所在的目錄一定要設成如下所示:file--->Project structtre-->moudles
如果不設置,就會找不到xml文件,或者可以直接放在resources下,但是不便於管理.
第八步:啟動項目測試
啟動這個類後,在瀏覽器輸入地址:成功後如下
user 和 getUserList是你在controller層定義的
springboot mybatis 整合