1.application.properties configuration information
Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/datebasenamespring.datasource.username= Rootspring.datasource.password=passwordspring.datasource.driver-class-name= Com.mysql.jdbc.Driver
2. Add Pom.xml
<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</a rtifactid> </dependency> <dependency> <groupid>org.springframework.boot</ groupid> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> < ;d ependency> <groupId>org.apache.ibatis</groupId> <artifactid>ibator</artifa ctid> <version>1.2.1.681</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> &L t;version>1.3.2</version> </dependency> <dependency> <groupid>org.myba Tis</groupid> <artifactId>mybatis</artifactId> <version>3.4.6</version> ; </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactid>hikaricp& Lt;/artifactid> <version>3.2.0</version> </dependency>
3. Database connection
ImportCom.zaxxer.hikari.HikariDataSource;Importorg.apache.ibatis.session.SqlSessionFactory;ImportOrg.mybatis.spring.SqlSessionFactoryBean;ImportOrg.mybatis.spring.annotation.MapperScan;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Primary;Importorg.springframework.stereotype.Component;ImportJavax.sql.DataSource;//loads SQL under the Com.example.demo.mapper.test directory and associates it to the current database@Component @mapperscan (value= "Com.example.demo.mapper.test", Sqlsessionfactoryref = "Sqlsessionfactorytest") Public classTestdatesource {//Read configuration file information@Value ("${spring.datasource.url}") PrivateString URL; @Value ("${spring.datasource.username}") PrivateString UserName; @Value ("${spring.datasource.password}") PrivateString password; @Value ("${spring.datasource.driver-class-name}") PrivateString Driverclassname; @Primary @Bean (Name= "Datesourcetest") PublicDataSource DataSource () {returngetdatasource (URL, userName, password, driverclassname); } //instantiation of@Bean (name = "Sqlsessionfactorytest") PublicSqlsessionfactory sqlsessionfactory (@Qualifier ("Datesourcetest") DataSource DS)throwsException {Sqlsessionfactorybean Sqlsessionfactorybean=NewSqlsessionfactorybean (); Sqlsessionfactorybean.setdatasource (DS); returnSqlsessionfactorybean.getobject (); } //The hikaridatasource connection pool is used here to define a connection pool Privatehikaridatasource getdatasource (string url, string userName, string password, string driverclassname) {FinalHikaridatasource ds =NewHikaridatasource (); Ds.setjdbcurl (URL); Ds.setusername (UserName); Ds.setpassword (password); Ds.setdriverclassname (Driverclassname); returnds; } }
4. Create the Mapper interface in the Com.example.demo.mapper.test directory, direct the data operation of the database
import org.apache.ibatis.annotations.*; Import Org.mapstruct.Mapper; Import Java.util.HashMap; Import java.util.List; @Mapper Public Interface Usermapper { @Select ("Select *from user") List<HashMap> getUser (); @Select ("Select *from user where Username=#{username}") List<HashMap> getusername (@ Param ("UserName") String userName);
5. The corresponding mapper interface on the service call
@Service Public classtestservice {@Resource usermapper usermapper; Publicjsonobject GetUSer () {jsonobject mapofcolvalues=NewJsonobject (); List<HashMap> order =Usermapper.getuser (); if(Collectionutils.isnotempty (order)) { for(inti = 0; I < order.size (); i++) {Mapofcolvalues.put ("id", Order.get (i). Get ("id"))); Mapofcolvalues.put ("User name", Order.get (i). Get ("user_name")); Mapofcolvalues.put ("Nickname", Order.get (i). Get ("Real_name")); Mapofcolvalues.put ("Phone number", Order.get (i). Get ("mobile"))); Mapofcolvalues.put ("Password", Order.get (i). Get ("Password")); } } Else{mapofcolvalues.put ("Result", "no Data"); } returnmapofcolvalues; }}
Springboot+mybatis+ibatis MySQL Link