A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 18037039769 于 2019-5-30 16:59 编辑

正题

Spring boot :2.1.5RELEASE ;数据库(Mysql、Oracle);Mybatis;阿里云的连接池 : Druid ;

步骤

1.POM依赖

[Java] 纯文本查看 复制代码
<!-- MyBatis -->[/size][/font][/color][/p]<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


[Java] 纯文本查看 复制代码
#Mybatis+MySQL+Druid
#Mysql时区异常URL后添加serverTimezone=GMT%2B8/连接池:typr/初始化连接:initialSize/最大空闲数:maxActive/最小空闲数:minIdle/获取连接等待时间:maxWait/最小等待时间:minEvictableIdleTimeMillis/关闭后不自动提交:defaultAutoCommit
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.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:test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.initialSize=20
spring.datasource.maxActive=50
spring.datasource.minIdle=10
#spring.datasource.maxWait=60000
#spring.datasource.minEvictableIdleTimeMillis=3600000
spring.datasource.defaultAutoCommit=false


3.其他文件生成

User.java

[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好用

[Java] 纯文本查看 复制代码
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

[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可以命名,主要还是看自己的日常使用

[AppleScript] 纯文本查看 复制代码
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;
 
@Service
public 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


[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;
 
@RestController
public 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是写接口


作者:沐荔

来源:CSDN

原文:https://blog.csdn.net/qq_41920732/article/details/85936063

版权声明:本文为博主原创文章,转载请附上博文链接!


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马