Spring: conquer databases (2), and spring conquer Databases
This section describes the integration of Spring and ORM frameworks. Although Hibernate is popular in the open-source ORM community, this article will take MyBatis as an example. In addition, the advantages and disadvantages of MyBatis and Hibernate are meaningless, mainly based on actual needs. If you are interested, you can view them on Baidu or Google.
First, configure the environment. You must have mybatis and mybatis-spring in the build path of the Spring project. If you are using Maven, you only need to add the following dependencies: (all are currently in the latest version)
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.7</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.2</version></dependency>
Here we mainly introduce the integration of Mybatis and Spring, and will not focus on the specific usage of Mybatis.
We know that the core of the MyBatis application is SqlSessionFactory, and such a bean needs to be defined in Spring,
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
SqlSessionFactoryBean is used to create SqlSessionFactory and inject the data source dataSource. The data source here is any data source defined in Spring. For demonstration convenience, we use the H2 embedded data source introduced in the previous article,
<jdbc:embedded-database id="dataSource" type="H2"><jdbc:script location="classpath:schema.sql" /><jdbc:script location="classpath:data.sql" /></jdbc:embedded-database>
In MyBatis, to implement SQL ing, the XML configuration file or mapper interface is used. In the previous article, the SQL mode is as follows:
create table spitter ( id identity, username varchar(25) not null, password varchar(25) not null, fullname varchar(100) not null, email varchar(50) not null, update_by_email boolean not null);
Create an interface,
package org.chen.mybatis.mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;import org.chen.domain.Spitter;public interface SpitterMapper {@Select("SELECT * from spitter where email = #{email}")Spitter getSpitter(@Param("email") String email);}
In fact, the name is not required, but the Convention is domain + Mapper. Add an annotation to a method. Here is @ Select. The content is an SQL statement and # {email} is a parameter for PrepraredStatement.
Then, use MapperFactoryBean to register the interface with Spring,
<bean id="spitterMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="org.chen.mybatis.mapper.SpitterMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
Now we can use the bean above to map the relational objects. We usually inject spitterMapper into the service,
For example,
public class TestService {private SpitterMapper spitterMapper;public void setSpitterMapper(SpitterMapper spitterMapper) {this.spitterMapper = spitterMapper;}public void getSpitterByEmail(String email){Spitter spitter = spitterMapper.getSpitter(email);System.out.println("spitter is " + spitter.getFullName());}}
Now, the basic operation can be completed.
Springmvc queries the binary image of the database and displays it in jsp.
First, do not store photos directly in the database, which is too troublesome and occupies space.
Only the name of the photo file is saved in the database.
For example, if the field name of the database is photo_name, the data in the database is
Photo_name
-----------------------
Jpg
Jpg
Jpg
Jpg and so on are all file names. The file itself is stored in another place on the server, for example, in the img/photo directory.
After this is done, you can directly display the image on the JSP page.
$ {User. photoName} is the photo file name you have taken from the database.
Springmvc queries the binary image of the database and displays it in jsp.
You can read the photo in the stream mode to obtain the file output stream of the photo. Use response in springMvc to get the output stream. Set the contentType format on the page to octec-stream, just like downloading the file, you can also display the front-end page with base64 code.