运行展示
正题
Spring boot :2.1.5RELEASE ;数据库(Mysql、Oracle);Mybatis;阿里云的连接池 : Druid ;
步骤
1.POM依赖
<!-- MyBatis --><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version></dependency> <!-- MySQL --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency> <!-- Oracle --><!--<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.4.0</version>/dependency>--> <!-- Druid --><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version></dependency>
2.application.properties
#Mybatis+MySQL+Druid#Mysql时区异常URL后添加serverTimezone=GMT%2B8/连接池:typr/初始化连接:initialSize/最大空闲数:maxActive/最小空闲数:minIdle/获取连接等待时间:maxWait/最小等待时间:minEvictableIdleTimeMillis/关闭后不自动提交:defaultAutoCommitspring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&serverTimezone=GMT%2B8&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false#spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.initialSize=20spring.datasource.maxActive=50spring.datasource.minIdle=10#spring.datasource.maxWait=60000#spring.datasource.minEvictableIdleTimeMillis=3600000spring.datasource.defaultAutoCommit=false
3. 其他文件生成
User.java
public class User { public Integer uid; public String uname; public String upassword; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUanme() { return uname; } public void setUanme(String uanme) { this.uname = uanme; } public String getUpassword() { return upassword; } public void setUpassword(String upassword) { this.upassword = upassword; } @Override public String toString() { return "User{" + "uid=" + uid + ", uname='" + uname + '\'' + ", upassword='" + upassword + '\'' + '}'; }}
UserDao.java
注解的形式,我感觉比XML好用
import com.spring.boot.bean.User;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update; import java.util.List; public interface UserDao { @Select("select * from user") public List<User> AllUser(); @Update("<script> " + "update user" + "<set>"+ "<if test='uname!=null'>uname=#{uname},</if>"+ "<if test='upassword!=null'>upassword=#{upassword},</if>"+ "</set>"+ "where uid=#{uid}"+ " </script> ") public int Update(User user); }
UserService.java
import com.spring.boot.bean.User;import java.util.List; public interface UserService { public List<User> AllUser(); public int Update(User user); }
UserImpl.java
主要是注解问题Service可以命名,主要还是看自己的日常使用
import com.spring.boot.bean.User;import com.spring.boot.dao.UserDao;import com.spring.boot.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service; import java.util.List; @Servicepublic class UserImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> AllUser() { return userDao.AllUser(); } @Override public int Update(User user) { return userDao.Update(user); }}
UserController.java
import com.spring.boot.bean.User;import com.spring.boot.service.impl.UserImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest; @RestControllerpublic class UserController { @Autowired private HttpServletRequest request; @Autowired private UserImpl userimpl; @RequestMapping("/api/login") public String Login(User user) { HttpSession session = request.getSession(); //存入Session //session.setAttribute("user", user); //单位为秒,设置为-1时不再失效 //session.setMaxInactiveInterval(60 * 60 * 24 * 30); //注销登陆 //request.getSession().removeAttribute("user"); return userimpl.UserAll(user).toString(); }}
注:userDao报红解决方法
注:启动类加入注解@MapperScan("路径)
这是Java web项目,@Controller注解是界面、@RestController是写接口
原文链接:SpringBoot - 整合Mybatis+Druid+数据库(注解版) - qq_41920732的博客 - CSDN博客