In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Basic environment
IDEA
MAVEN
Engineering catalogue
Mysql database
Maven Pom
Add dependent packages for mybatis and mysql
4.0.0 com.mu mybatis 1.0-SNAPSHOT org.mybatis mybatis 3.4.1 mysql mysql-connector-java 5.1.29 src/main/resources * * / * .properties * * / .xml * * / * .tld false src/main/java * * / .properties * * / .xml * * / .tld false create entity classes and mapper.xml files
User.javapackage com.mu.mybatis.domain;/** * Created by manunited1985 on 2017-11-23. * / public class User {private Integer userId; private String userName; private String userPassword; private String userEmail; public User (Integer userId, String userName, String userPassword, String userEmail) {this.userId = userId; this.userName = userName; this.userPassword = userPassword; this.userEmail = userEmail;} public Integer getUserId () {return userId;} public void setUserId (Integer userId) {this.userId = userId } public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName;} public String getUserPassword () {return userPassword;} public void setUserPassword (String userPassword) {this.userPassword = userPassword;} public String getUserEmail () {return userEmail;} public void setUserEmail (String userEmail) {this.userEmail = userEmail @ Override public String toString () {return "User [userId=" + userId + ", userName=" + userName + ", userPassword=" + userPassword + ", userEmail=" + userEmail + "]";}}
UserMapper.javapackage com.mu.mybatis.mapper;import com.mu.mybatis.domain.User;/** * Created by manunited1985 on 2017-11-24. * / public interface UserMapper {public User selectUserById (Integer userId); public List selectUserAll (); public void insertUser (User user);}
UserMapper.xml SELECT * FROM t_user WHERE USER_ID = # {userId} SELECT * FROM t_user insert into t_user (USER_ID,USER_NAME,USER_PASSWORD,USER_EMAIL) values (# {userId}, # {userName}, # {userPassword}, # {userEmail})
Main.javapackage com.mu.mybatis;import com.mu.mybatis.domain.User;import com.mu.mybatis.mapper.UserMapper;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;import java.io.IOException;import java.util.List;/** * Created by manunited1985 on 2017-11-23. * / public class Main {public static void main (String [] args) throws IOException {SqlSession session = null; try {/ / read configuration information String resource = "application.xml"; InputStream inputStream = Resources.getResourceAsStream (resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder (); / / Select a different environment String env = "product" SqlSessionFactory factory = builder.build (inputStream,env); / / generate SqlSession object session = factory.openSession (); System.out.println (session); / / get mapping object UserMapper userMapper = session.getMapper (UserMapper.class) / / get accounts with id 1 User user = userMapper.selectUserById (1); System.out.println (user); / / get all accounts List arrayList = userMapper.selectUserAll (); System.out.println (arrayList);} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace () } finally {if (session! = null) / / close the connection session.close ();}
Database configuration jdbc.propertiesjdbc_driver=com.mysql.jdbc.Driverjdbc_url=jdbc\: mysql\: / / 127.0.0.1\: 3306/test?useUnicode=true&characterEncoding=utf-8jdbc_username=rootjdbc_password=123456
Apply configuration application.xml
Run the result org.apache.ibatis.session.defaults.DefaultSqlSession@454b70a9User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com] [User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com], User [userId=2, userName=tom, userPassword=123, userEmail=tom@sina.com], User [userId=3, userName=jack, userPassword=123, userEmail=jack@sina.com] remarks
If you use a project created by IDEA, a similar phenomenon occurs when loading the mapper.xml file:
Mybatis error-- java.io.IOException: Could not find resource com/xxx/xxxMapper.xml
Then, you can add the following fields to the pom.xml file:
Src/main/java * * / .xml
IDEA is the xml file that does not compile src's java directory, so the xml file cannot be found in Mybatis's configuration file! (it may also be the problem with the Maven build project. Many of the online tutorials are ordinary Java web projects, so you can put them under src and read them.)
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: 0
*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.