Spring整合Mybatis
1、导入pom坐标
<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><!-- https://mvnrepository.com/artifact/c3p0/c3p0 --><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.16</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency>
- 这些都是mybatis需要引用的依赖坐标
2、SpringConfig配置类
@Configuration
@ComponentScan({"com.itheima"})
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {}
- @Configuration 配置类注解
- @ComponentScan 扫描路径
- @PropertySource 配置文件
- @Import 需要导入的 配置文件
3、JdbcConfig配置类
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;@Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(username);ds.setPassword(password);return ds;}}
-
从配置文件中读取配置项,注入到Bean里面
3、 MybatisConfig配置类
public class MybatisConfig {@Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;}@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itheima.dao");return msc;}}
- 按示例导入,不明所以
4、Dao接口
@Repository
public interface AccountDao {@Insert("insert into tbl_account(name, money)values(#{name},#{money})")void save(Account account);@Update("update tbl_account set name = ${name}, money=${money} where id = #{id}")void update(Account account);@Delete("delete from tbl_account where id = #{id}")void delete(Integer id);@Select("select * from tbl_account")List<Account> findAll();@Select("select * from tbl_account where id = #{id}")Account findById(Integer id);
}
- @Repository Bean实例
- @Insert @Update @Delete @Select @Select 不明所以
5、Account实体类
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;}@Overridepublic String toString(){return "Account{" +"id="+id+", name='"+name+ '\'' +", money=" + money+"}";}
}
6、AccountService接口和实现类
public interface AccountService {void save(Account account);void update(Account account);void delete(Integer id);Account findById(Integer id);List<Account> findAll();}@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate 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();}
}
-
@Autowired 自动装配 AccountDao
7、使用方法
public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService = ctx.getBean(AccountService.class);Account ac = accountService.findById(1);System.out.println(ac);}