一:导入依赖
在我们创建一个Maven项目时,可以在pom.xml文件下导入以下依赖:
1.mybatis依赖
1. <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency>
2.spring-jdbc
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.31</version> </dependency>
3.mybatis-spring
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency>
二:建立配置类
0.
前置工作
在dao包和service包下建立有关的接口和实现类
dao包:
package com.lcyy.dao; 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 AccountDao { @Insert("insert into tbl_account(name,money)values(#{name},#{money})") void save(Account account); @Delete("delete from tbl_account where id = #{id} ") void delete(Integer id); @Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ") void update(Account account); @Select("select * from tbl_account") List<Account> findAll(); @Select("select * from tbl_account where id = #{id} ") Account findById(Integer id); }
package com.lcyy.dao; public class Account { private Integer id; private String name; private String money; public Account(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public Account(Integer id, String name, String money) { this.id = id; this.name = name; this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money='" + money + '\'' + '}'; } }
service包:
package com.lcyy.service; import com.lcyy.dao.Account; import java.util.List; public interface AccountService { void save(Account account); void delete(Integer id); void update(Account account); List<Account> findAll(); Account findById(Integer id); }
package com.lcyy.service.impl; import com.lcyy.dao.Account; import com.lcyy.dao.AccountDao; import com.lcyy.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class AccountServiceImpl implements AccountService { @Autowired private AccountDao accountDao; public void save(Account account) { accountDao.save(account); } public void update(Account account){ accountDao.update(account); } public void delete(Integer id) { accountDao.delete(id); } public Account findById(Integer id) { return accountDao.findById(id); } public List<Account> findAll() { return accountDao.findAll(); } }
1.
在config包下建立一个MybatisConfig的类
2.
创建JdbcConfig配置DataSource数据源
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false jdbc.username=root jdbc.password=zhien0516
package com.lcyy.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import javax.sql.DataSource; //@Configuration public class JdbcConfig { // @Value("druid") // private String age; @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("dataSource") public DataSource dataSource(){ DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); // System.out.println("我是德鲁伊"+age); return ds; } }
3.
创建MybatisConfig整合mybatis
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class MybatisConfig { //创建工厂 @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){ SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setTypeAliasesPackage("com.lcyy.damain"); sessionFactoryBean.setDataSource(dataSource); return sessionFactoryBean; } //扫包 @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setBasePackage("com.lcyy.dao"); return mapperScannerConfigurer; } }
4.
定义测试类进行测试
import com.lcyy.config.SpringConfig; import com.lcyy.dao.Account; import com.lcyy.service.AccountService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class DataSource { public static void main(String[] args) { //获取ioc容器 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); //根据类型获取bean,并强制转换为DataSource类型 // Object ctxBean = ctx.getBean("dataSource"); // System.out.println(ctxBean); AccountService ctxBean = ctx.getBean(AccountService.class); Account ac = ctxBean.findById(1); System.out.println("ac = " + ac); } }
5.
测试结果
数据库:
三:总结
以上就是用spring整合mybatis的步骤!希望对读者有所帮助,后续我将继续更新有关spring的知识!!!