搭建SpringBoot+Mybaties0配置

依赖:


        org.mybatis
        mybatis-spring
        1.3.2


       org.springframework.boot
       spring-boot-starter-jdbc
       2.1.2.RELEASE


        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            mysql
            mysql-connector-java
            8.0.15
        

这里需要注意的是
对应的数据库版本一定要和依赖对应上
比如


image


image

两个的版本要对应上

然后在配置类中


@Configuration
@MapperScan("com.***.****.dao")
public class MyWebMVCConfig implements WebMvcConfigurer {

    @Bean("dataSource")
    public DataSource dataSource(){
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        driverManagerDataSource.setUrl("jdbc:mysql://IP地址:3306/数据库名字?useSSL=false&serverTimezone=Asia/Shanghai");
        driverManagerDataSource.setUsername("root");
        driverManagerDataSource.setPassword("密码");
        return driverManagerDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        return sqlSessionFactoryBean.getObject();
    }

}


创建Mapper

这里创建的类名规范应该是Lxy_visitMapper,应该注意。

@Mapper
public interface Lxy_visitDao {

    @Insert("INSERT INTO lxy_visit VALUES(#{id},#{ipAddress},#{visTime},#{orders},#{address},#{content_address}," +
            "#{city},#{city_code},#{district},#{province},#{province_code},#{street},#{street_number},#{point_x},#{point_y})")
    @SelectKey(keyProperty = "id", resultType = String.class, before = true,
            statement = "select replace(uuid(), '-', '') as id from dual")
    int saveLxy_visit(Lxy_visit lxy_visit);

    @Select("SELECT orders FROM lxy_visit ORDER BY orders DESC LIMIT 1")
    Integer getLxy_visitOrders();

}

然后就完成了!

你可能感兴趣的:(搭建SpringBoot+Mybaties0配置)