今天运用mybaits来进行mysql操作:
连接数据库信息:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--别名--> <typeAliases> <package name="user"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!--mysql 连接信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="数据库连接名"/> <property name="username" value="用户名"/> <property name="password" value="密码"/> </dataSource> </environment> </environments> <mappers> <package name="mapper"/> </mappers> </configuration>
bean类
package bean; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public User(String username, String password) { this.username = username; this.password = password; } }
持久层:
package mapper; import bean.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper { @Select("select * from tb_user") List<User> selectAll(); @Select("select * from tb_user where username=#{username}") List<User> selectByName(String username); }
服务层:
package res; import bean.User; import 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.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class UserRes { public static List<User> findAll() throws IOException { List<User> list = new ArrayList<>(); String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); list = userMapper.selectAll(); sqlSession.close(); return list; } public static List<User> selectByName(String name) throws IOException { List<User> list = null; String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); list = userMapper.selectByName(name); sqlSession.close(); return list; } }
标签:username,String,05,List,2023,import,password,public,14 From: https://www.cnblogs.com/JJTyyds/p/17400243.html