In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
MyBatis
@ [toc]
Brief introduction benefit
The biggest advantage is that the SQL statement is flexible and suitable for tuning scenarios and complex business scenarios.
Inferior position
The biggest disadvantage is the migration between different databases.
Introduction of mybatis components
Add to pom.xml
Mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1
Add to application.properties
# dateSourcespring.datasource.driverClassName=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=falsespring.datasource.username=rootspring.datasource.password=root# mybatis# underline turning hump on mybatis.configuration.map-underscore-to-camel-case=true# mapper scan position mybatis.mapper-locations=classpath:mapper/*.xml
To be clear, the mysql driver I introduced is com.mysql.cj.jdbc.Driver, not com.mysql.jdbc.Driver. The following are descriptions of some parameters
ServerTimezone: this driver needs to specify a time zone. UseUnicode: enables the specified encoding. CharacterEncoding: specifies the encoding to read the database, because the project uses UTF-8, and the database information is accessed consistently. UseSSL: whether to establish a SSL connection or not, I choose no, because it provides a truststore for server certificate verification, and there are no conditions for it. Code actual combat
The local mysql version is 5.7.20.
Construction table sentence
CREATE TABLE USER_INFO (user_id DECIMAL (10) PRIMARY KEY NOT NULL, user_name VARCHAR (20) DEFAULT "" NOT NULL); CREATE UNIQUE INDEX USER_INFO_user_id_uindex ON USER_INFO (user_id); ALTER TABLE USER_INFO COMMENT = 'user basic information table'
Insert data
INSERT INTO test.user_info (user_id, user_name) VALUES (1, 'Trump'); INSERT INTO test.user_info (user_id, user_name) VALUES (2, 'Donald')
The current project path is
Add the @ MapperScan annotation to the entry class of SpringBoot to scan the DAO class instead of adding a @ Mapper annotation to each dao interface.
Package com.example;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan ("com.example.dao") public class DemoApplication {public static void main (String [] args) {SpringApplication.run (DemoApplication.class, args);}}
Note: if there is no split project micro-service architecture or distributed architecture, @ SpringBootApplication in the application main class will automatically scan @ Controller,@Service,@Resource in this package, and there is no need to waste another @ ComponentScan annotation to configure the path.
UserController.java
Package com.example.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.example.service.IUserService;@Controller@RequestMapping ("user") public class UserController {@ Autowired private IUserService userService; @ RequestMapping ("/ queryUser") @ ResponseBody void queryUser () {this.userService.queryUser ();}}
IUserService.java
Package com.example.service;public interface IUserService {void queryUser ();}
UserServiceIml.java
Package com.example.service;import com.example.dao.UserDao;import com.example.entity.UserEntity;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;@Service ("userService") public class UserServiceIml implements IUserService {@ Resource private UserDao userDao; @ Override public void queryUser () {List userList = this.userDao.queryUser (); System.out.println ("="); System.out.println (userList) System.out.println ("=");}}
Note: if you are from the SSM architecture, this @ Resource annotation may be unfamiliar, because I expect to use @ Autowired as before, but I did not use @ Repository annotation in the dao layer. Previously, I used @ MapperScan to scan all dao automatically in the main class, so I can no longer use the previous @ Autowired.
UserDao.java
Package com.example.dao;import com.example.entity.UserEntity;import java.util.List;public interface UserDao {List queryUser ();}
UserEntity.java
Package com.example.entity;public class UserEntity {private long userId; private String userName; public long getUserId () {return userId;} public void setUserId (long userId) {this.userId = userId;} public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName } @ Override public String toString () {return "UserEntity {" + "userId=" + userId + ", userName='" + userName +'\'+'}';}}
If you do not run successfully, please compare the version of the introduced package, and whether the database configuration is based on your local, by the way, see if the comments are the same as in the article.
The effect picture is as follows
Note: if you follow this tutorial, because spring security was introduced at the beginning, and the new path is not in the permissions just now, so I changed it to be accessible to all zs users. If you don't configure spring security as before, don't worry about the following
/ / http.authorizeRequests () .antMatchers ("/ user/addUser") .hasRole ("AAA") http.authorizeRequests () .antMatchers ("/ * *") .hasRole ("AAA")
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 251
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.