Springboot+mybatis+ibatis mysql連結

來源:互聯網
上載者:User

標籤:連結   example   ica   user   mobile   maps   ice   resource   rop   

 1.application.properties 配置資訊

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.添加pom.xml

<dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-jdbc</artifactId>        </dependency>        <dependency>            <groupId>org.apache.ibatis</groupId>            <artifactId>ibator</artifactId>            <version>1.2.1.681</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.3.2</version>        </dependency>        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.4.6</version>        </dependency>        <dependency>            <groupId>com.zaxxer</groupId>            <artifactId>HikariCP</artifactId>            <version>3.2.0</version>        </dependency>

 

3.資料庫連接

import com.zaxxer.hikari.HikariDataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;import javax.sql.DataSource;//載入com.example.demo.mapper.test目錄下的sql,並關聯到當前到資料庫@Component@MapperScan(value = "com.example.demo.mapper.test", sqlSessionFactoryRef = "sqlSessionFactoryTest")public class TestDateSource {    //讀取設定檔資訊    @Value("${spring.datasource.url}")    private String url;    @Value("${spring.datasource.username}")    private String userName;    @Value("${spring.datasource.password}")    private String password;    @Value("${spring.datasource.driver-class-name}")    private String driverClassName;    @Primary    @Bean(name = "dateSourceTest")    public DataSource dataSource() {        return getDataSource(url, userName, password, driverClassName);    }    //執行個體化    @Bean(name = "sqlSessionFactoryTest")    public SqlSessionFactory sqlSessionFactory(@Qualifier("dateSourceTest") DataSource ds) throws Exception {        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();        sqlSessionFactoryBean.setDataSource(ds);        return sqlSessionFactoryBean.getObject();    }    //這裡用到了HikariDataSource串連池,定義一個串連池    private HikariDataSource getDataSource(String url, String userName, String password, String driverClassName) {        final HikariDataSource ds = new HikariDataSource();        ds.setJdbcUrl(url);        ds.setUsername(userName);        ds.setPassword(password);        ds.setDriverClassName(driverClassName);        return ds;    }    }

4.在com.example.demo.mapper.test目錄下建立mapper介面,直接進行資料庫的資料操作

import org.apache.ibatis.annotations.*;import org.mapstruct.Mapper;import java.util.HashMap;import java.util.List;@Mapperpublic interface UserMapper {    @Select("select *from user")    List<HashMap> getUser();    @Select("select *from user where username=#{userName}")    List<HashMap> getuserName(@Param("userName") String userName);  }

5.在service調用對應的mapper介面

@Servicepublic class TestService {    @Resource    UserMapper userMapper;    public JSONObject getUSer() {        JSONObject mapOfColValues = new JSONObject();        List<HashMap> order = userMapper.getUser();        if (CollectionUtils.isNotEmpty(order)) {            for (int i = 0; i < order.size(); i++) {                mapOfColValues.put("id", order.get(i).get("id"));                mapOfColValues.put("使用者名稱", order.get(i).get("user_name"));                mapOfColValues.put("暱稱", order.get(i).get("real_name"));                mapOfColValues.put("手機號", order.get(i).get("mobile"));                mapOfColValues.put("密碼", order.get(i).get("password"));            }        } else {            mapOfColValues.put("result", "沒有資料");        }        return mapOfColValues;    }}

 

Springboot+mybatis+ibatis mysql連結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.