目录
1创建MyBatis注解的数据层接口AccountDao.class
只需要写一个MyBatis配置类就行了
一定有三个配置文件
先创建一个简单的Spring案例
- use mybatis;
- create table tbl_account
- (
- id INT(11),
- name VARCHAR(25),
- money double
- );
- insert into tbl_account (id, name, money)
- values (1,'郭浩康',100);
- select *
- from tbl_account;
- package com.itheima.domain;
-
- import java.io.Serializable;
-
- public class Account implements Serializable {
-
- private Integer id;
- private String name;
- private Double money;
-
- 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 Double getMoney() {
- return money;
- }
-
- public void setMoney(Double money) {
- this.money = money;
- }
-
- @Override
- public String toString() {
- return "Account{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", money=" + money +
- '}';
- }
- }
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false
- jdbc.username=root
- jdbc.password=root
- package com.itheima.config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- 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(){
- DruidDataSource ds = new DruidDataSource();
- ds.setDriverClassName(driver);
- ds.setUrl(url);
- ds.setUsername(userName);
- ds.setPassword(password);
- return ds;
- }
- }
/*MybatisConfig * 连个bean * SqlSessionFactoryBean和代理MapperScannerConfigurer * */
- package com.itheima.config;
-
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.mapper.MapperScannerConfigurer;
- import org.springframework.context.annotation.Bean;
-
- import javax.sql.DataSource;
-
- /*MybatisConfig
- * 连个bean
- * SqlSessionFactoryBean和代理MapperScannerConfigurer
- * */
- public class MybatisConfig {
- //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
- @Bean
- public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
- SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();
- ssfb.setTypeAliasesPackage("com.itheima.domain");
- ssfb.setDataSource(dataSource);
- return ssfb;
- }
- //
- //定义bean,返回MapperScannerConfigurer对象
- @Bean
- public MapperScannerConfigurer mapperScannerConfigurer(){
- MapperScannerConfigurer msc = new MapperScannerConfigurer();
- msc.setBasePackage("com.itheima.dao");
- return msc;
- }
- }
-
-
-
- package com.itheima.dao;
-
- import com.itheima.domain.Account;
- 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
findAll(); -
- @Select("select * from tbl_account where id = #{id} ")
- Account findById(Integer id);
- }
- package com.itheima.service;
-
- import com.itheima.domain.Account;
-
- import java.util.List;
-
- public interface AccountService {
-
- void save(Account account);
-
- void delete(Integer id);
-
- void update(Account account);
-
- List
findAll(); -
- Account findById(Integer id);
-
- }
- package com.itheima.service.impl;
-
- import com.itheima.dao.AccountDao;
- import com.itheima.domain.Account;
- import com.itheima.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
findAll() { - return accountDao.findAll();
- }
- }
- package com.itheima.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;
-
- //SpringConfig创建步骤(4步)
- // 申明配置类@Configuration
- //扫描包@ComponentScan("com.itheima")
- //加载jdbc文件@PropertySource("classpath:jdbc.properties")
- //导入jdbc类@Import({JdbcConfig.class,MybatisConfig.class})
- @Configuration
- @ComponentScan("com.itheima")
- //@PropertySource:加载类路径jdbc.properties文件
- @PropertySource("classpath:jdbc.properties")
- @Import({JdbcConfig.class,MybatisConfig.class})
- public class SpringConfig {
-
- }
- import com.itheima.config.SpringConfig;
- import com.itheima.domain.Account;
- import com.itheima.service.AccountService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
- public class App2 {
- public static void main(String[] args) {
- //加载Spring配置文件
- ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
-
- AccountService accountService = ctx.getBean(AccountService.class);
-
- Account ac = accountService.findById(1);
- System.out.println(ac);
- }
- }