依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
这里需要注意的是
对应的数据库版本一定要和依赖对应上
比如
和
两个的版本要对应上
然后在配置类中
@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();
}
然后就完成了!