基于注解与XML的MyBatis+Spring开发
基于注解的Spring开发
涉及内容:
- JDK11
- mysql
- maven
- Mybatis
- Spring
涉及知识点:
- 创建表格,添加数据
- MyBatis基于注解和XML的开发
- XML配置文件:配置数据源;事务类型
@Select:执行sql语句,根据方法返回值类型,返回对象
- Spring基于注解的开发
- 创建对象:
- @Component :在核心容器中创建一个对象
- @Controller @Service @Repository:对应-表现层,服务层,持久层
- 注入数据
- @Autowired :注入数据,按照类型自动注入
- @Qualifier :在自动注入的基础上根据id注入=>value:用具指定bean的ID
- @Resource:直接按照id注入,只能注入其他bean类型=>name:用于指定bean的ID
- @Value:注入基本类型数据和String类型
- 改变作用范围
- @Scope:指定 bean 的作用范围=>
singleton prototype(常用) request session globalsession
- @Scope:指定 bean 的作用范围=>
- 生命周期相关
- @PostConstruct:用于指定初始化方法
- @PreDestroy:用于指定销毁方法
- 创建对象:
- Spring配置相关的注解
- @Configuration :指定配置类,取代配置文件
- @ComponentScan:basePackages属性=>指定创建容器时要扫描的包
- @Bean:只能写在方法上;使用此方法创建一个对象,并放入容器=>name属性:指定bean的id
- @PropertySource:用于加载
.properties文件中的配置;如果在类路径下,需要写上classpath:在被注解的配置类中用@Value("${键名}")来取值 - @Import:导入其他配置类
首先应该创建一个MySQL数据库

然后创建对应的实体类;
这里只创建一个部门表;
package gx.domain;
import org.springframework.stereotype.Component;
@Component("dept")
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
配置MyBatis,这里配置文件取名为 SqlConfig.XML
<?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>
<!--配置别名-->
<typeAliases>
<package name="gx.dao.domain"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="gx.dao"/>
</mappers>
</configuration>
编写Spring的配置类
package gx.config;
import gx.dao.IDeptDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import java.io.IOException;
import java.io.InputStream;
@Configurable//声明这是一个配置类
@ComponentScan(basePackages = "gx")//要扫描的包
public class SpringConfig {
@Bean(name = "jdbcStrs1")
public String CreateJDBC(@Qualifier("jdbcStrs2") String str){
return str+"此时,@Qualifier可以单独使用;";
}
@Bean(name = "jdbcStrs2")
public String CreateJDBC2(){
return "通过@bean创建一个对象;\n";
}
}
编写查询数据的接口
package gx.dao;
import gx.domain.Dept;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 数据库相关的操作
*/
//开启二级缓存
@CacheNamespace(blocking = true)
public interface IDeptDao {
/**
* 查询所有
* @return
*/
@Select("select * from dept")
List<Dept> findAllDept();
}
写实现类,实现查询功能,用于Services层调用
package gx.domain;
import org.springframework.stereotype.Component;
@Component("dept")
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
接下来即使services层的编写
package gx.servers;
import gx.domain.Dept;
import java.util.List;
public interface IDeptService {
List<Dept> findAllDept();
}
package gx.servers.Impl;
import gx.dao.IDeptDao;
import gx.domain.Dept;
import gx.servers.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptService implements IDeptService {
@Autowired
IDeptDao deptDao = null;
public List<Dept> findAllDept() {
return deptDao.findAllDept();
}
}
表现层
package gx.ui;
import gx.config.SpringConfig;
import gx.domain.Dept;
import gx.servers.IDeptService;
import gx.servers.Impl.DeptService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.IOException;
import java.util.List;
public class Client {
public static ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
public static void main(String[] args) throws IOException {
DeptService deptService = ac.getBean("deptService", DeptService.class);
for (Dept d:deptService.findAllDept())
System.out.println(d);
String jdbcStrs1 = ac.getBean("jdbcStrs1", String.class);
System.out.print(jdbcStrs1);
}
}
运行结果:

结果无误;这个警告是因为JDK版本导致的

浙公网安备 33010602011771号