一、准备工作 新建maven项目 连接mysql数据库 修改pom.xml: 1.设置打包方式为jar
<groupId>com.atguigu.mybatis</groupId>
<artifactId>MyBatis_demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
2.导入mysql、junit、mybatis、log4j依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
把log4j.xml放在resources下
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="true">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
二、pojo与mapper pojo
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String sex;
private String email;}
mapper接口
public interface UserMapper {
int insertUser();
void updateUser();
void deleteUser();
User getUserById();
List<User> getAllUser();
}
三、创建映射文件模板 1、jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root
2、核心配置文件mybatis-config.xml
<?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>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="com.atguigu.mybatis.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.atguigu.mybatis.mapper"/>
</mappers>
</configuration>
3、映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.mapper.UserMapper">
<insert id="insertUser">
insert into t_user values (null,'admin',123456,23,'男','12345@qq.com')
</insert>
<update id="updateUser">
update t_user set username='张四' where id = 4
</update>
<delete id="deleteUser">
delete from t_user where id=5
</delete>
<select id="getUserById" resultType="User">
select * from t_user where id=3
</select>
<select id="getAllUser" resultType="User">
select * from t_user
</select>
</mapper>
四、SqlSessionUtils工具类和测试 SqlSessionUtil
public class SqlSessionUtils {
public static SqlSession getSqlSession(){
SqlSession sqlSession=null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
测试
public class ParameterMapperTest {
@Test
public void testgetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> list = mapper.getAllUser();
list.forEach(user -> System.out.println(user));
}
}
五、MyBatis获取参数
接口
public interface ParameterMapper {
User checkLogin(String username,String password);
User checkLoginByMap(Map<String,Object>map);
int insertUser(User user);
User checkLoginByParam(@Param("username") String username,@Param("password") String password);
User GetUserByUsername(String username);
List<User> getAllUser();
}
映射
<select id="getAllUser" resultType="User">
select * from t_user
</select>
<select id="GetUserByUsername" resultType="User">
select * from t_user where username='${name}'
</select>
<select id="checkLogin" resultType="User">
select * from t_user where username=#{arg0} and password=#{arg1}
</select>
<select id="checkLoginByMap" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email});
</insert>
<select id="checkLoginByParam" resultType="User">
select * from t_user where username=#{username} and password=#{password}
</select>
测试
@Test
public void testgetUserByUsername(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.GetUserByUsername("admin");
System.out.println(user);
}
@Test
public void testLogin(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.checkLogin("admin","123456");
System.out.println(user);
}
@Test
public void testCheckLoginByMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
Map<String,Object> map=new HashMap<>();
map.put("username","admin");
map.put("password","123456");
User user = mapper.checkLoginByMap(map);
System.out.println(user);
}
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
int result=mapper.insertUser(new User(null,"李9","32323",23,"男","6328378@qq.com"));
System.out.println(result);
}
@Test
public void testcheckLoginByParam(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user=mapper.checkLoginByParam("admin","123456");
System.out.println(user);
}
@Test
public void testgetAllUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> list = mapper.getAllUser();
list.forEach(user -> System.out.println(user));
}
@Test
public void testJDBC() throws Exception {
String username="admin";
Class.forName("");
Connection connection= DriverManager.getConnection("","","");
PreparedStatement ps = connection.prepareStatement("select * from t_user where username=?");
ps.setString(1,username);
}
|