SpringBoot+Mybatis xml版

项目结构:

SpringBoot+Mybatis xml版_第1张图片

application.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=1993920ai

server.port=8081
#spring.http.encoding.force=true
#spring.http.encoding.charset=UTF-8
#spring.http.encoding.enabled=true
#server.tomcat.uri-encoding=UTF-8

实体类:

public class User implements Serializable{

     private int id;
     private String name;
     private String password;
}

配置类:

@ComponentScan("yiche.com")
@Configuration
@PropertySource("classpath:application.properties")
public class MyConfig {


    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
     @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;


          /*
            如果没有上面的 也可以使用这个注解
            //覆盖默认数据源 使用druid数据源
            @ConfigurationProperties(prefix = "spring.datasource")

            */
            
     @Bean
     @Primary
    public DataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;



    }



    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){

        SqlSessionFactoryBean sqlFactory = new SqlSessionFactoryBean();
        try{
            sqlFactory.setDataSource(dataSource());
            //设置mybatis的主配置文件  记住是主配置文件啊
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource mybatisXml = resolver.getResource("classpath:mybatis-config.xml");

            sqlFactory.setConfigLocation(mybatisXml);

            //设置mapper.xml文件的路径

            Resource[] resource2=resolver.getResources("classpath:yiche/com/mapper/*.xml");
            sqlFactory.setMapperLocations(resource2);

            //扫描实体类所在包
            sqlFactory.setTypeAliasesPackage("yiche.com.bean");
        }catch (Exception e){
            //e.printStackTrace();
        }
        return sqlFactory;

    }


/*
    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        ConfigurableEmbeddedServletContainer factory = new TomcatEmbeddedServletContainerFactory();
        factory.setDocumentRoot(new File("D:\\WorkSpace\\SpringBoot_1\\src\\main\\webapp\\"));
        return (EmbeddedServletContainerFactory) factory;
    }*/



    //mapper接口扫描器  也可以使用注解@MapperScan(basePackages = "yiche.com.mapper") 替代如下
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        //mapper接口所在的包
        mapperScannerConfigurer.setBasePackage("yiche.com.mapper");

        return mapperScannerConfigurer;
    }

  //可以不配置mapper扫描器但是在启动类上需要加@MapperScan注解 如下
  //@ComponentScan("yiche.com")
 //@MapperScan("yiche.com.mapper")
}


// /*事务管理*/    //也可以不配置使用默认的
             @Bean
             public DataSourceTransactionManager getDataSourceTransactionManager1() {
                   return new DataSourceTransactionManager(dataSource());
             }

接口:定义增删改查的方法

@Repository
 public interface UserMapper1 {


       List getAll();


       User getByName(String name);


       int delete(User user);


       int update(User user);


       int addUser(User user);


}

接口.xml文件
UserMapper.xml








    
    


    

    
        insert into
        user
        

            
                name,
            

            
                password,
            


        
        
            
                #{name}, jdbcType=VARCHAR},
            

            
                #{password,jdbcType=VARCHAR},
            

        
    


    
        update user
        
            
                name=#{name},
            

        
        where id=#{id}
    


    
		delete from user where id=#{id}
	







Controller:

@Controller
public class UserController {

     @Autowired
     private UserDao userDao;

 
    @RequestMapping("/add")

    public String add(){

        User user=new User(12,"tom","123");
        userDao.addUser(user);

        return "success";
    }

    @RequestMapping("/delete")
    public String delete(){
        User user=new User(1,"tom","123");
        userDao.delete(user);
        return "success";
    }


    @RequestMapping("/getByName")
    @ResponseBody
  public String getByName(){

        System.out.println("ok");
        User user=userDao.getByName("tom");
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/update")
    public String update(){
        User user=new User(2,"cc","123");
        userDao.update(user);
        return "success";
    }


    @RequestMapping("/getAll")
    @ResponseBody
    public String getAll(org.apache.coyote.Request request){
        List users=userDao.getAll();
        request.setAttribute("user",users);
        System.out.println(users.size());
        return "list";
    }

}

Mybatis全局文件:

 mybatis-config.xml
 




pom.xml文件:



        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
    
    
   
        
            org.springframework.boot
            spring-boot-starter
        


        
        
            org.springframework.boot
            spring-boot-starter-web
            1.5.6.RELEASE
        
        

        


        
            org.springframework
            spring-jdbc
            4.3.10.RELEASE
        

        
        
            org.mybatis
            mybatis
            3.4.0
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
            mysql
            mysql-connector-java
            5.1.40
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            com.alibaba
            druid
            1.0.18
        
        

        
        
            com.google.code.gson
            gson
            2.8.0
        





        
            com.github.iweinzierl
            jsonformat
            1.0
        

        
            org.json
            json
            20131018
        

        
        
            org.springframework
            spring-test
            3.1.1.RELEASE
            test
        

         
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

你可能感兴趣的:(Mybatis)