0
点赞
收藏
分享

微信扫一扫

SpringBoot+mybatis入门

闲嫌咸贤 2022-11-21 阅读 99


SpringBoot+mybatis入门_mysql

 

SpringBoot+mybatis入门_spring_02

SpringBoot+mybatis入门_spring_03

SpringBoot+mybatis入门_spring_04

检查maven插件

SpringBoot+mybatis入门_sql_05

​​http://maven.apache.org/download.cgi​​

设置自己的maven地址

SpringBoot+mybatis入门_mysql_06

 

@RestController

表示接受前台请求

首次未设置pom中的 mybatis可以先注释其中的依赖部分

然后 import changes  即可

SpringBoot+mybatis入门_mysql_07

访问8080/hello  即可

package com.hzp.demo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class hello { @RequestMapping(value="/hello",method=RequestMethod.GET) public String hello(){ return "rrr"; } }

SpringBoot+mybatis入门_mysql_08

修改端口号

server.context-path=/demo

必须加/demo/hello才能访问

SpringBoot+mybatis入门_sql_09

使用sequel pro

 

 

CREATE TABLE tb_area (

area_id int(2) NOT NULL auto_increment,

area_name varchar(200) NOT NULL,

priority int(2) NOT NULL DEFAULT'0',

create_time datetime DEFAULT NULL,

last_edit_time datetime DEFAULT NULL,

PRIMARY KEY(area_id),

UNIQUE KEY UK_AREA(area_name)

)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

SpringBoot+mybatis入门_mysql_10

option+enter

 

SpringBoot+mybatis入门_mysql_11

自动提示倒入类

 

SpringBoot+mybatis入门_spring_12

 

 

control+ N

SpringBoot+mybatis入门_sql_13

SpringBoot+mybatis入门_sql_14

为所有成员变量增加 getter  setter 方法

现在取消pom中mybatis的注释

加入

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>

因为之前设置了mysql表

<dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency>

加入多线程处理 线程池

SpringBoot+mybatis入门_mysql_15

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局属性 --> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="true"/> </settings> </configuration>

SpringBoot+mybatis入门_mysql_16

option+command 或者 option +回车 设置红字

设置config.dao的包

设置链接数据库的代码

package com.hzp.demo.config.dao; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.annotation.Bean; import java.beans.PropertyVetoException; public class DateSourceConfiguration { private String jdbcDriver; private String jdbcUrl; private String jdbcUsername; private String jdbcPassword; @Bean(name = "dataSource") public ComboPooledDataSource createDateSource() throws PropertyVetoException { ComboPooledDataSource dataSource=new ComboPooledDataSource(); dataSource.setDriverClass(jdbcDriver); dataSource.setJdbcUrl(jdbcUrl); dataSource.setUser(jdbcUsername); dataSource.setPassword(jdbcPassword); //关闭链接后不自动提交 dataSource.setAutoCommitOnClose(false); return dataSource; } }

SpringBoot+mybatis入门_mysql_17

写入

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/dbName?useUnicode=true&characterEncoding=utf8&useSSL=false jdbc.username=root jdbc.password=root

 

同样在datasourceconfig引入变量  在前面加入 @Value("jdbc.$")

@Configuration

@Value("jdbc.driver")private String jdbcDriver; @Value("jdbc.url") private String jdbcUrl; @Value("jdbc.username") private String jdbcUsername; @Value("jdbc.password") private String jdbcPassword;

SpringBoot+mybatis入门_mysql_18

 

/usr/local/opt/mysql@5.7/bin/mysql.server start

 

  启动完成后,打开终端

  ​​alias​​​ ​​mysql=​​​​/usr/local/mysql/bin/mysql​

​  alias​​​ ​​mysqladmin=​​​​/usr/local/mysql/bin/mysqladmin​

mysqladmin -u root -p password root123

SpringBoot+mybatis入门_mysql_19

package com.hzp.demo.config.dao;import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.beans.factory.annotation.Autowired; 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.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; import java.io.IOException; @Configuration public class SessionFactoryConfiguration { @Value("${mybatis_config_file}") private String mybatisConfigFilePath; @Value("${mapperPath}") private String mapperPath; @Value("${entity_package}") private String entityPackage; @Autowired @Qualifier("dataSource") private DataSource dataSource; @Bean("name=sqlSessionFactory") public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath)); PathMatchingResourcePatternResolver resolver= new PathMatchingResourcePatternResolver(); String packageSearchPath=PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath; sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath)); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage); return sqlSessionFactoryBean; } }

SpringBoot+mybatis入门_sql_20

#mybatismybatis_config_file=mybatis-config.xml mapper_path=/mapper/**.xml entity_package=com.hzp.demo.entity

SpringBoot+mybatis入门_sql_21

package com.hzp.demo.dao;import com.hzp.demo.entity.Area; import java.util.List; public interface AreaDao { List<Area> queryArea(); Area queryAreaById(int areaId); int insertArea(Area area); int updateArea(Area area); int deleteArea(Area areaId); }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

举报

相关推荐

0 条评论