Spring Tips: Spring Native and MyBatis

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips this time we're looking at mybatis and now i know we did look at this way back in 2017 and we're not going to intentionally retread that same path what we want to do this time is to look at the spring native support for mybatis and spring boot over the holidays i decided i wanted to start hacking on the spring and my base integration uh seeing if i could find some way to provide a nice integration for spring native and growl vm anybody who has watched the show before surely knows that growl vm is a drop-in replacement for the hotspot just-in-time compiler uh it is a just-in-time compiler that takes your code that frequently executes and turns it into native architecture specific and by you know operating system specific binaries those binaries are much smaller in footprint and in startup time than your typical jre application but to achieve these results it has to forsake some of the many dynamic behaviors uh that we take for granted on the java platform things like reflection proxies uh you know serialization i mean jni access all this kind of stuff that you just kind of take for granted no longer works in the context of a gravity on native images without at least some configuration and that's where spring native comes in that spring native provides configuration that makes it so if you're just creating objects that do none of these dynamic things it'll just work out of the box because all the dynamic behavior will be in spring and spring native makes spring work in that context the trouble lies when you have libraries or integrations that you use that themselves also have dynamic behavior things like reflection like creating proxies like serialization like jni etc somebody needs to write extra programmatic hints to discover those dynamic integrations and tell the gravime compiler about that this is one of the wheel houses for spring native now spring native bear in mind is a a project that we created a few years ago uh it has uh proved to be a very useful way to get spring boot 2x and spring framework 5x generation applications to work with graviam native images i put together a prototype that was far from complete and then my beta's team took notice and they eventually started expanding upon it and taking it further eventually fellow spring team member stefan nicole was nice enough to lend his time to help take the example even further to take the integration even further and eventually we had something that actually did what we're hoping to do right you can take your spring boot my business applications unchanged except for the build and get them to work in a gravity and native image context that's awesome that is such good news uh and so i wanted to show that to you today i think that'll be a lot of fun and the result hopefully will speak for itself in terms of memory footprint and startup time let's take a look all right let's build a new application we'll go as always to my second favorite place on the internet start that spring leo and here we're going to generate a new application using the latest and greatest we're going to call this my betas native i'll use java 17 because i can and it's the future and i should i'll use h2 which is an in-memory sql data store i'll use mybatis i'm going to use lombok to make short work of some of the stuff that i still need to do because unfortunately as far as i can tell my business doesn't support java 17 records um it's okay fine i i'm happy to have native images we'll get there eventually but in the meantime uh i gotta do that so my betaist lumbac a sql database um i think i don't know if i need it but i'll just bring it jdbc just in case you know you can never be too sure uh and then of course what spring native don't we so spring native gives us our ability to create native images we'll hit generate and we'll open this up in our ide okay we have a brand new project let's make sure that we've got all the dependencies required because again we have the default mybatis springwood auto configuration but we don't have the spring native support so there's mybatis the spring boot starter there's spring native there's h2 there's lumbuck we need uh the myobitus spring native core so let's add that here okay so there we are there's the my beta spring native core the spring boot my base springboot starter that should do it that should be everything we need let's go ahead and build a very simple application that uses these new types so we'll go to source we'll go to a mybatis application and we're going to build an application that has a simple entity we'll call that um customer and for some reason this type needs to be public i don't like it when it has to be public but here we are so okay good actually we don't need that actually if we have that and we'll have private final integer id private final string name private final string email all right good and we want a repo a mapper right so we'll create a new type here called customer mapper and this is going to be an interface at mapper we'll have a few methods here to actually handle the data access and the things that we're trying to do so we'll have a method here called void insert customer customer and another one called customer find by id loan id okay now my betas won't know what to do with these abstract methods because there's no implementation and the methods don't tell us anything of themselves we need to provide some annotations so insert into customer name email values name email parentheses good okay and we want options we want to use the generated keys and the key property will be id okay then we want to do a select so select id name email from customer or id equals id and obviously the parameter here corresponds to the name of the parameter here these fields these properties name and email are derived from the pojo customer that we pass in and now finally we can actually write something that runs when the application starts up okay we're going to create a customer mapper and we will write some data so our customer equals new customer and uh one and it'll just be josh josh joshlong.com okay mapper dot insert customer and var result equals mapper.findbycustomer.id and you know let's just prove that it has worked okay result that's our customer and then uh find result okay all right i want to run this the problem is it won't work because it's expecting a sql schema to be there and that schema hasn't yet been set up so let's create a little schema.sql file to have fields for our customer entity okay remember we have a table called customer primary key and two columns called name and email that are of type for varcar so we'll go here source main resources schema.sql create table customer id serial primary key name var car 255 not no email varacar 255 not no all right all right there's our basic application let's run it and see what we get started the application in you know less than half a second this is on the jre mind you uh inserted a record got an id and then found the record back so that's working great okay good so i'm able to write data and then find it by its id and get it back in the same basic shape as i as i put it in there now let's turn this into a gravium native image to do that we're going to go to a command line and then use the basic built-in support so my base native maven d to skip the test because that'll double the length of the compilation p native clean package so that's going to use the maven profile for native it's going to package the application up into first.jar and then native then a native application binary so that's going to use the gravion native image compiler it's going to package the application up into first a jar and then to a native image this process could take a minute or two so now is a good time to go get some coffee actually there's always a good time to go get caught [Music] all right the application is finished it took a minute and a half to compile let's go to the target directory and we'll just run it and see what we get there we are there's our application and that completed in 40 milliseconds so literally 10 times difference right we ran it on the jre and it ran it started up in 419 milliseconds or just a little bit more than four tenths of a second we ran it in a native image and started up in 0.04 milliseconds so 040 is how you think of that right 40 milliseconds about 10 times faster uh and the best part is if it hadn't just linked into existence and then disappeared if it if it actually had kept a process open uh we could have measured its memory footprint but i'm sure you would have found it it would have been you know similarly small right a 50 megs of ram instead of 500 or whatever your typical jerry application uses these days all right my friends that's it i mean i just wanted you to see that the work is amazing go check it out uh try your spring boot and my base applications out and send feedback to the mybatis project this is good stuff it's exciting and i can't wait to be able to use this without even having to set anything up just you had the spring boot starter and there's native hints for spring framework six built in out of the box pretty cool huh short and sweet now bear in mind that this stuff is intended for spring native which is the best way to build spring boot 2x and spring framework 5x generation applications and get them working in a gravium native image context that's it spring framework six and springboot three are just around the corner due to ship sometime later this year in 2022. these new reversions these new versions these new revisions are a new generation and they will have built-in i hesitate to say native support for building native images using graphvm it'll be in the core of spring framework and in the spring boot build time tooling and integrations uh not in spring native so conceptually i expect that a lot of what we do here in spring native will work fairly well in that new context but i can't say for sure that the types themselves work out of the box very likely they won't right there might be some package changes or indeed some new concepts or different concepts that appear between now and net that said that work should fall to us the developers of the integrations that you're using in your application not to you dear user if we do our job correctly you should be able to take your spring boot application using mybatis change the build to reflect the latest and greatest and have it just work both on the gre and in a native image context it's a very exciting time to be alive and it's a good time to try applications now that integration for mybatis and springboot is not yet ga of course so it would be great to get your feedback to get your validation and that'll help shape the way this thing works now with spring native and in the future with spring framework 6 and springboot 3. as always my friends thanks for watching and we'll talk to you next time
Info
Channel: SpringDeveloper
Views: 7,123
Rating: undefined out of 5
Keywords: Web Development (Interest), spring, pivotal, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere Application Server (Software), IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, spring cloud
Id: EWWq3ts9Tv4
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Wed Feb 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.