Record springboot using Druid and MyBatis configuration

Source: Internet
Author: User

1.Druid Introduction is not professional, Baidu self-examination

Main configurations:

To configure the file Application.properties first:

server.port=8081 #MVC配置 spring.mvc.view.prefix = Classpath:/templates/spring.mvc.view.suffix =. HTML #静态资源配置在conf/ Webmvcconfig #使用Mysql spring.datasource.type = Com.alibaba.druid.pool.DruidDataSource SPRING.DATASOURCE.URL=JDBC:
Mysql://localhost:3307/website Spring.datasource.username=root spring.datasource.password=  
Spring.datasource.driverclassname=com.mysql.jdbc.driver #连接池的配置信息 spring.datasource.initialsize=5  
Spring.datasource.minidle=5 spring.datasource.maxactive=20 spring.datasource.maxwait=60000  
spring.datasource.timebetweenevictionrunsmillis=60000 spring.datasource.minevictableidletimemillis=300000  
Spring.datasource.validationquery=select 1 from DUAL spring.datasource.testwhileidle=true  
Spring.datasource.testonborrow=false Spring.datasource.testonreturn=false  
Spring.datasource.poolpreparedstatements=true spring.datasource.maxpoolpreparedstatementperconnectionsize=20 Spring.datasource.filters=stat,wall,log4j Spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000 #Mybatis mybatis.config-locations=classpath:mybatis/ Mybatis-config.xml Mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package= Com.damionew.website.model
Then several classes

Druiddbconfig

Import java.sql.SQLException;

Import Javax.sql.DataSource;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.beans.factory.annotation.Value;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.Primary;  
Import Com.alibaba.druid.pool.DruidDataSource;  
Import Com.alibaba.druid.support.http.StatViewServlet; 
Import Com.alibaba.druid.support.http.WebStatFilter;  
      
    @Configuration public class Druiddbconfig {private Logger Logger = Loggerfactory.getlogger (Druiddbconfig.class);  
      
    @Value ("${spring.datasource.url}") Private String Dburl;  
      
    @Value ("${spring.datasource.username}") private String username;  
      
    @Value ("${spring.datasource.password}") private String password;  
      
    @Value ("${spring.datasource.driverclassname}") Private String driverclassname; @Value ("${spring.dAtasource.initialsize} ") private int initialsize;  
      
    @Value ("${spring.datasource.minidle}") private int minidle;  
      
    @Value ("${spring.datasource.maxactive}") private int maxactive;  
      
    @Value ("${spring.datasource.maxwait}") private int maxwait;  
      
    @Value ("${spring.datasource.timebetweenevictionrunsmillis}") private int timebetweenevictionrunsmillis;  
      
    @Value ("${spring.datasource.minevictableidletimemillis}") private int minevictableidletimemillis;  
      
    @Value ("${spring.datasource.validationquery}") Private String validationquery;  
      
    @Value ("${spring.datasource.testwhileidle}") Private Boolean testwhileidle;  
      
    @Value ("${spring.datasource.testonborrow}") Private Boolean testonborrow;  
      
    @Value ("${spring.datasource.testonreturn}") Private Boolean Testonreturn; @Value ("${spring.datasource.poolpreparedstatements}") Private BooLean poolpreparedstatements; @Value ("${spring.datasource.maxpoolpreparedstatementperconnectionsize}") Private int  
      
    Maxpoolpreparedstatementperconnectionsize;  
      
    @Value ("${spring.datasource.filters}") Private String filters;  
      
    @Value ("{spring.datasource.connectionProperties}") Private String connectionproperties;  
        @Bean//Declare that it is a Bean instance @Primary//In the same DataSource, first use the DataSource public DataSource DataSource () {  
          
        Druiddatasource DataSource = new Druiddatasource ();  
        Datasource.seturl (This.dburl);  
        Datasource.setusername (username);  
        Datasource.setpassword (password);  
          
        Datasource.setdriverclassname (Driverclassname);  
        Configuration datasource.setinitialsize (initialsize);  
        Datasource.setminidle (Minidle);  
        Datasource.setmaxactive (maxactive);  
        Datasource.setmaxwait (maxwait); Datasource.settimebetWeenevictionrunsmillis (Timebetweenevictionrunsmillis);  
        Datasource.setminevictableidletimemillis (Minevictableidletimemillis);  
        Datasource.setvalidationquery (Validationquery);  
        Datasource.settestwhileidle (Testwhileidle);  
        Datasource.settestonborrow (Testonborrow);  
        Datasource.settestonreturn (Testonreturn);  
        Datasource.setpoolpreparedstatements (poolpreparedstatements);  
        Datasource.setmaxpoolpreparedstatementperconnectionsize (maxpoolpreparedstatementperconnectionsize);  
        try {datasource.setfilters (filters);  
        catch (SQLException e) {logger.error ("Druid Configuration Initialization Filter", e);  
          
        } datasource.setconnectionproperties (ConnectionProperties);  
    return datasource; }  
}
Druidstatfilter
Import Javax.servlet.annotation.WebFilter;  
Import Javax.servlet.annotation.WebInitParam;  
  
Import Com.alibaba.druid.support.http.WebStatFilter;  
  
@WebFilter (filtername= "Druidwebstatfilter", urlpatterns= "/*",  
    initparams={  
        @WebInitParam (name= " Exclusions ", value=" *.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* ")//Ignore resource  
   }  
) public  
class Druidstatfilter extends Webstatfilter {  
  
}  
Druidstatviewservlet

@WebServlet (urlpatterns= "/druid/*",  
    initparams={  
         @WebInitParam (name= "Allow", value= ""),//IP White list (not configured or empty , all access is allowed)  
         @WebInitParam (name= "Deny", value= ""),//IP blacklist (when in common, the deny takes precedence over allow)  
         @WebInitParam (name= " Loginusername ", value=" root "),//username  
         @WebInitParam (name=" Loginpassword ", value=" 123 "),//password  
         @WebInitParam ( Name= "Resetenable", value= "false")//Disable the "Reset All" feature on the HTML page  
) public  
class Druidstatviewservlet extends Statviewservlet {  
    private static final long serialversionuid = -2688872071445249539l;  
  
}  

So, wait for the http://localhost:8081/druid/login.html login when the account password is root,123

Another is to add @servletcomponentscan to the application class.

@ServletComponentScan
@EnableWebMvc
@SpringBootApplication
@MapperScan ("Com.damionew.website.dao") Public
class Application extends webmvcconfigureradapter{public
	static void Main (string[] args) {
		Springapplication.run (Application.class, args);
	}

This will display the database monitoring page, the page is druid generated


Then MyBatis configuration, Heartache, when appearing

Invalid bound statement (not found): Com.damionew.website.dao.UserDao.insert

When, checked for a long time, in the XML configuration file finally found, mybatis.mapper-locations

Handwriting, the hands are missing s.

The details are also the http://blog.csdn.net/gebitan505/article/details/54929287 with the demo, using XML configuration

Feel as long as the profile path is correct, the namespace is correct, there is nothing wrong with it


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.