欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot与Mybatis整合

程序员文章站 2024-01-26 15:04:40
...

步骤一:创建一个maven项目,我们看看整体结构

SpringBoot与Mybatis整合


步骤二:在pom.xml中添加依赖

<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了 -->
    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>
  
    <properties>
		<!-- 指定jdk版本号 -->
		<java.version>1.7</java.version>
    </properties>
	
	<dependencies>
        <!-- springboot web 依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web </artifactId>
		</dependency>
		

		<!-- mybatis-springboot整合 -->
		<dependency>
		    <groupId>org.mybatis.spring.boot </groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.3.0</version>
		</dependency>
		
		<!-- spring-webmvc -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-webmvc</artifactId>
		    <version>4.3.12.RELEASE</version>
		</dependency>
		
	  <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-web</artifactId>
	    <version>4.3.14.RELEASE </version>
	  </dependency>
	

步骤三:编写启动类

@SpringBootApplication             //SpringBoot入口
@ComponentScan("com.springboot")  //扫描所有注解信息,自动配置
@MapperScan("com.springboot.*") //扫描mybatis的配置信息
public class App {
   public static void main(String[] args) {
	 SpringApplication.run(App.class, args);
  }
}

步骤四:编写配置信息application.properties

spring.datasource.driverClassName = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@10.20.20.240:1521:whtest
spring.datasource.username = ****
spring.datasource.password =*****

步骤五:编写DTO

public class Person {
  private Integer Id ;
  private String name;
  private Integer age;
public Integer getId() {
	return Id;
}
public void setId(Integer id) {
	Id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Integer getAge() {
	return age;
}
public void setAge(Integer age) {
	this.age = age;
}
}

步骤六:编写PersonMapper接口类, (里面包括所有增删改查接口方法)

@Mapper
public interface PersonMapper {
	
	@Insert("insert into person(id,name,age)values(#{id},#{name},#{age})")
	//获取自增长id(可返回客户端)
	@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
        void insert(Person person);
	
	@Delete("delete from person where id=#{id} ")
      Long delete(Person person);
	
	@Update("update person set name=#{name},age=#{age} where id=#{id}")
       Long update(Person person);
    
       @Select("select * from person where id=#{id}")
       Person selectById(Long id);
     
       @Select("select * from person where name like #{name}")
       List<Person> likeName(String name);

      @Select("select * from person")
       List<Person> selectAll();
    
}

步骤七:Service层建立。通过@Autowired注入PersonMapper,调用其中方法。

@Service
public class PersonService {
	
	@Autowired
	private PersonMapper personMapper;
  
	public List<Person> likeName(String name){
	  return personMapper.likeName(name);
	  
	}
	public Person selectById(Long id) {
		return personMapper.selectById(id);
	}
	public List<Person> selectAll() {
		return  personMapper.selectAll();
	}
       public void save(Person person){
     	 personMapper.insert(person);
      }
}

步骤八:控制层建立(注入service ,调用service方法)

@Controller
public class PersonController {
    @Autowired
	private PersonService personService;
	
    @ResponseBody
    @RequestMapping("/likeName")
    public List<Person> likeName(String name){
    	return personService.likeName(name);
    }
    @ResponseBody
    @RequestMapping("/selectById")
    public Person selectById(Long id){
    	return personService.selectById(id);
    }   

     
    @ResponseBody
    @RequestMapping(value="/save",method=RequestMethod.GET)
    public Person save(){
    	Person p=new Person();
    	p.setId(10);
    	p.setName("李四");
    	p.setAge(20);
        personService.save(p);	
    	return p;
    } 
}


最后,运行一***意:张三是字符串但是不用加引号

SpringBoot与Mybatis整合