• Springboot+mybatis+ibatis mysql链接


     1.application.properties 配置信息

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/datebasename
    spring.datasource.username=root
    spring.datasource.password=password
    spring.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;
    
    @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.在service调用对应的mapper接口

    @Service
    public 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;
    
        }
    }
  • 相关阅读:
    将CMD内的显示内容输出到txt文件
    上传项目到Github
    利用百度地图API,在浏览器中找到自己的位置
    操作系统相关知识
    字符串的全排列
    二维数组做函数参数、指向指针的指针做函数参数
    strstr函数与strcmp函数
    华为机试题——数组排序,且奇数存在奇数位置,偶数存在偶数位置
    单链表的建立,插入,显示,查找删除以及反转
    《深入了解 Linq to SQL》之对象的标识 —— 麦叔叔呕心呖血之作
  • 原文地址:https://www.cnblogs.com/mkmoney/p/9599329.html
Copyright © 2020-2023  润新知