快速入门
DROP TABLE IF EXISTS user ;
CREATE TABLE user
(
id BIGINT ( 20 ) NOT NULL COMMENT '主键ID' ,
name VARCHAR ( 30 ) NULL DEFAULT NULL COMMENT '姓名' ,
age INT ( 11 ) NULL DEFAULT NULL COMMENT '年龄' ,
email VARCHAR ( 50 ) NULL DEFAULT NULL COMMENT '邮箱' ,
PRIMARY KEY ( id)
) ;
INSERT INTO user ( id, name, age, email) VALUES
( 1 , 'Jone' , 18 , 'test1@baomidou.com' ) ,
( 2 , 'Jack' , 20 , 'test2@baomidou.com' ) ,
( 3 , 'Tom' , 28 , 'test3@baomidou.com' ) ,
( 4 , 'Sandy' , 21 , 'test4@baomidou.com' ) ,
( 5 , 'Billie' , 24 , 'test5@baomidou.com' ) ;
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.0.5</ version>
</ dependency>
spring :
profiles :
active : dev
datasource :
driver-class-name : com.mysql.cj.jdbc.Driver
url : jdbc: mysql: //localhost: 3306/mybatis_plus? useSSL=false&useUnicode =true&characterEncoding =utf- 8&serverTimezone =GMT%2B8
username : root
password : 123456
logging :
level :
com.baomidou.mybatisplus.samples.quickstart : debug
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
public interface UserMapper extends BaseMapper < User> {
}
@SpringBootApplication
@MapperScan ( "com.baomidou.mybatisplus.samples.quickstart.mapper" )
public class QuickstartApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( QuickstartApplication. class , args) ;
}
}
@RunWith ( SpringRunner. class )
@SpringBootTest
public class SampleTest {
@Resource
private UserMapper userMapper;
@Test
public void testSelect ( ) {
System. out. println ( ( "----- selectAll method test ------" ) ) ;
List< User> userList = userMapper. selectList ( null) ;
Assert. assertEquals ( 5 , userList. size ( ) ) ;
userList. forEach ( System. out: : println) ;
}
}
代码生成器
public class KuangCode {
public static void main ( String[ ] args) {
AutoGenerator mpg = new AutoGenerator ( ) ;
GlobalConfig gc = new GlobalConfig ( ) ;
String projectPath = System. getProperty ( "user.dir" ) ;
gc. setOutputDir ( projectPath+ "/src/main/java" ) ;
gc. setAuthor ( "cl" ) ;
gc. setOpen ( false ) ;
gc. setFileOverride ( false ) ;
gc. setServiceName ( "%sService" ) ;
gc. setIdType ( IdType. ID_WORKER) ;
gc. setDateType ( DateType. ONLY_DATE) ;
gc. setSwagger2 ( true ) ;
mpg. setGlobalConfig ( gc) ;
DataSourceConfig dsc = new DataSourceConfig ( ) ;
dsc. setUrl ( "jdbc: mysql: / / localhost: 3306 / mybatis_plus?
useSSL= false & useUnicode= true & characterEncoding= utf- 8 & serverTimezone= GMT% 2 B8") ;
dsc. setDriverName ( "com.mysql.cj.jdbc.Driver" ) ;
dsc. setUsername ( "root" ) ;
dsc. setPassword ( "123456" ) ;
dsc. setDbType ( DbType. MYSQL) ;
mpg. setDataSource ( dsc) ;
PackageConfig pc = new PackageConfig ( ) ;
pc. setModuleName ( "blog" ) ;
pc. setParent ( "com.cl" ) ;
pc. setEntity ( "entity" ) ;
pc. setMapper ( "mapper" ) ;
pc. setService ( "service" ) ;
pc. setController ( "controller" ) ;
mpg. setPackageInfo ( pc) ;
StrategyConfig strategy = new StrategyConfig ( ) ;
strategy. setInclude ( "blog_tags" , "course" , "links" , "sys_settings" , "user_record" , "
user_say") ;
strategy. setNaming ( NamingStrategy. underline_to_camel) ;
strategy. setColumnNaming ( NamingStrategy. underline_to_camel) ;
strategy. setEntityLombokModel ( true ) ;
strategy. setLogicDeleteFieldName ( "deleted" ) ;
TableFill gmtCreate = new TableFill ( "gmt_create" , FieldFill. INSERT) ;
TableFill gmtModified = new TableFill ( "gmt_modified" ,
FieldFill. INSERT_UPDATE) ;
ArrayList< TableFill> tableFills = new ArrayList < > ( ) ;
tableFills. add ( gmtCreate) ;
tableFills. add ( gmtModified) ;
strategy. setTableFillList ( tableFills) ;
strategy. setVersionFieldName ( "version" ) ;
strategy. setRestControllerStyle ( true ) ;
strategy. setControllerMappingHyphenStyle ( true ) ;