ibatis text mysql_Springboot+mybatis+ibatis+mysql

1.application.properties 配置信息

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/datebasenamespring.datasource.username=root

spring.datasource.password=password

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2.添加pom.xml

mysql

mysql-connector-java

org.springframework.boot

spring-boot-starter-jdbc

org.apache.ibatis

ibator

1.2.1.681

org.mybatis

mybatis-spring

1.3.2

org.mybatis

mybatis

3.4.6

com.zaxxer

HikariCP

3.2.0

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")publicclass 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")

publicSqlSessionFactory 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) {

finalHikariDataSource ds =new HikariDataSource();

ds.setJdbcUrl(url);

ds.setUsername(userName);

ds.setPassword(password);

ds.setDriverClassName(driverClassName);

return ds;

}

}

4.在com.example.demo.mapper.test目录下创建mapper接口,直接进行数据库的数据操作

importorg.apache.ibatis.annotations.*;import org.mapstruct.Mapper;import java.util.HashMap;import java.util.List;

@Mapperpublicinterface UserMapper {

@Select("select *from user")

List getUser();

@Select("select *from user where username=#{userName}")

List getuserName(@Param("userName") String userName);

}

5.在service调用对应的mapper接口

@Servicepublicclass TestService {

@Resource

UserMapper userMapper;

public JSONObject getUSer() {

JSONObject mapOfColValues =new JSONObject();

List order = userMapper.getUser();

if (CollectionUtils.isNotEmpty(order)) {

for(inti = 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值