Integration phase
Since Springboot does not have a quick start assembly for Beatlsql, I need to import the relevant beans myself, including the data source, package scan, things manager and so on.
Add the following code to application:
@Bean (Initmethod = "Init", name = "Beetlconfig") public beetlgrouputilconfiguration getbeetlgrouputilconfiguration () { Beetlgrouputilconfiguration beetlgrouputilconfiguration = new Beetlgrouputilconfiguration (); Resourcepatternresolver patternresolver = resourcepatternutils.getresourcepatternresolver (new Defaultresourceloader ()); The try {//Webappresourceloader configuration root path is critical webappresourceloader Webappresourceloader = new Webappreso Urceloader (Patternresolver.getresource ("Classpath:/templates"). GetFile (). GetPath ()); Beetlgrouputilconfiguration.setresourceloader (Webappresourceloader); } catch (IOException e) {e.printstacktrace (); }//Read configuration file information return beetlgrouputilconfiguration; } @Bean (name = "Beetlviewresolver") public beetlspringviewresolver Getbeetlspringviewresolver (@Qualifier ("Beetlconfi G ") Beetlgrouputilconfiguration beetlgrouputilconfiguration) {Beetlspringviewresolver BeeTlspringviewresolver = new Beetlspringviewresolver (); Beetlspringviewresolver.setcontenttype ("Text/html;charset=utf-8"); Beetlspringviewresolver.setorder (0); Beetlspringviewresolver.setconfig (beetlgrouputilconfiguration); return beetlspringviewresolver; }//Configuration package scan @Bean (name = "Beetlsqlscannerconfigurer") public beetlsqlscannerconfigurer Getbeetlsqlscannerconfigurer () {Beetlsqlscannerconfigurer conf = new Beetlsqlscannerconfigurer (); Conf.setbasepackage ("Com.forezp.dao"); Conf.setdaosuffix ("Dao"); Conf.setsqlmanagerfactorybeanname ("Sqlmanagerfactorybean"); return conf; } @Bean (name = "Sqlmanagerfactorybean") @Primary public Sqlmanagerfactorybean Getsqlmanagerfactorybean (@Qualifier ("DataSource") DataSource DataSource) {Sqlmanagerfactorybean factory = new Sqlmanagerfactorybean (); Beetlsqldatasource Source = new Beetlsqldatasource (); Source.setmastersource (DataSource); FactoRy.setcs (source); Factory.setdbstyle (New Mysqlstyle ()); Factory.setinterceptors (New Interceptor[]{new Debuginterceptor ()}); FACTORY.SETNC (New Underlinednameconversion ());//Open Hump Factory.setsqlloader (new Classpathloader ("/sql");//sql file path return factory; }//Metabase @Bean (name = "DataSource") public DataSource Getdatasource () {return datasourcebuilder.create () . URL ("Jdbc:mysql://127.0.0.1:3306/test"). Username ("root"). Password ("123456"). Build (); }//Turn on transaction @Bean (name = "Txmanager") public Datasourcetransactionmanager Getdatasourcetransactionmanager (@Qualifier ("DataSource") DataSource DataSource) {Datasourcetransactionmanager DSM = new Datasourcetransactionmanager (); Dsm.setdatasource (DataSource); return DSM; }
Under Resouces package, add Meta_inf folder, add spring-devtools.properties to folder:
Restart.include.beetl=/beetl-2.3.2.jarrestart.include.beetlsql=/beetlsql-2.3.1.jar
Add a Index.btl file under Templates.
After adding the jar and configuring these beatlsql beans, and the resources of these configurations, Springboot will be able to access the database classes.
Give me a restful chestnut table for initializing a database
# DROP table ' account ' IF existscreate table ' account ' ( ' id ' int (one) ' Not NULL auto_increment, ' name ' varchar (20) Not NULL, ' money ' double default NULL, PRIMARY KEY (' id ')) engine=innodb auto_increment=4 default Charset=utf8;i Nsert into ' account ' values (' 1 ', ' aaa ', ' + '); INSERT into ' account ' values (' 2 ', ' BBB ', ' + '); INSERT INTO ' account ' V Alues (' 3 ', ' CCC ', ' 1000 ');
Bean
public class Account { private int id; private String name; private double money; Getter ... Setter ... }
Data Access DAO Layer
Public interface Accountdao extends basemapper<account> { @SqlStatement (params = ' name ') account Selectaccountbyname (String name);}
Interfaces inherit Basemapper, you can get some properties of a single table query, and when you need to customize SQL, just write the file under the Resouses/sql/account.md file:
selectaccountbyname===* according to name, select * from account where name= #name #
where "= = =" Above is a unique identifier, corresponding to the interface method name, "*" followed by comments, in the following is the custom SQL statement, see the official document.
Web tier
The service layer is omitted and the actual development is made up.
@RestController @requestmapping ("/account") public class AccountController {@Autowired Accountdao Accountdao; @RequestMapping (value = "/list", method = requestmethod.get) public list<account> getaccounts () {return ACC Ountdao.all (); } @RequestMapping (value = "/{id}", method = Requestmethod.get) public account Getaccountbyid (@PathVariable ("id") int ID) {return accountdao.unique (ID); } @RequestMapping (value = "", method = Requestmethod.get) public account Getaccountbyid (@RequestParam ("name") String Name) {return accountdao.selectaccountbyname (name); } @RequestMapping (value = "/{id}", method = requestmethod.put) public String Updateaccount (@PathVariable ("id") int ID , @RequestParam (value = "name", required = True) String name, @RequestParam (value = "Money", required = true) Double Mone Y) {account account=new account (); Account.setmoney (Money); Account.setname (name); Account.setid (ID); int T=acCountdao.updatebyid (account); if (t==1) {return account.tostring (); }else {return "fail"; }} @RequestMapping (value = "", method = requestmethod.post) public String postaccount (@RequestParam (value = "NA Me ") String name, @RequestParam (value =" money ") double money) {Account account = N EW account (); Account.setmoney (Money); Account.setname (name); Keyholder t = accountdao.insertreturnkey (account); if (T.getint () > 0) {return account.tostring (); } else {return "fail"; } }
Source Source
Through the postman test, the code has all passed.
Enterprise distribution Micro-service cloud Springcloud springboot MyBatis (20) Integration Beatlsql