spring整合Mybatis | Postgresql为例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合Mybatis | Postgresql为例相关的知识,希望对你有一定的参考价值。
1.创建配置文件jdbc.properties
jdbc.url=jdbc:postgresql://localhost:5432/postgis_hy?useSSL=false
jdbc.username=postgres
jdbc.password=arcgis
2.相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
3.目录结构
a.配置类
// SpringConfig.java
package com.hye.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("com.hye")
@PropertySource("classpath:jdbc.properties")
@Import(JDBCConfig.class,MybatisConfig.class)
public class SpringConfig
// JDBCConfig.java
package com.hye.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class JDBCConfig
@Value("$jdbc.driver")
private String driver;
@Value("$jdbc.url")
private String url;
@Value("$jdbc.username")
private String username;
@Value("$jdbc.password")
private String password;
@Bean
public DataSource dataSource()
PGSimpleDataSource pg = new PGSimpleDataSource();
pg.setUrl(url);
pg.setUser(username);
pg.setPassword(password);
return pg;
// MybatisConfig.java
package com.hye.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource)
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("com.hye.entity");
ssfb.setDataSource(dataSource);
return ssfb;
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.hye.dao");
return msc;
b.接口类
package com.hye.dao;
import com.hye.entity.Person;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface PersonDao
@Insert("INSERT INTO test.posttest(name, age, sex) VALUES (#name,#age,#sex)")
void save(Person person);
@Delete("delete from test.posttest where name=#name")
void delete(String name);
@Update("update test.posttest set name = #name,age = #age,sex = #sex where name = #name")
void update(Person person);
@Select("select * from test.posttest")
List<Person> findAll();
@Select("select * from test.posttest where name = #name")
Person findByName(String name);
c.对象类
package com.hye.entity;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
public class Person implements Serializable
@Autowired
private String name;
@Autowired
private Integer age;
@Autowired
private String sex;
@Override
public String toString()
return "Person"+
"姓名="+name+
",年龄="+age+
",性别="+sex+
"";
d.服务类
// PersonService.java
package com.hye.service;
import com.hye.entity.Person;
import java.util.List;
public interface PersonService
void save(Person person);
void delete(String name);
void update(Person person);
List<Person> findAll();
Person findByName(String name);
// PersonServiceImpl.java
package com.hye.service.impl;
import com.hye.dao.PersonDao;
import com.hye.entity.Person;
import com.hye.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService
@Autowired
private PersonDao personDao;
public void save(Person person)
personDao.save(person);
public void update(Person person) personDao.update(person);
public void delete(String name) personDao.delete(name);
public Person findByName(String name)
return personDao.findByName(name);
public List<Person> findAll()
return personDao.findAll();
e.主程序
package com.hye;
import com.hye.config.SpringConfig;
import com.hye.entity.Person;
import com.hye.service.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import java.util.List;
public class App
public static void main(String[] args)
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
PersonService p = ctx.getBean(PersonService.class);
Person all = p.findByName("小明");
System.out.println(all);
四、数据及运行结果
a.数据表
b.运行结果
箴言:因为这些东西是非常简单的。不要抱怨自己学不会,那是因为你没有足够用心。
以上是关于spring整合Mybatis | Postgresql为例的主要内容,如果未能解决你的问题,请参考以下文章