大家好,我是【1+1=王】, 热爱java的计算机(人工智能)渣硕研究生在读。
如果你也对java、人工智能等技术感兴趣,欢迎关注,抱团交流进大厂!!!
Good better best, never let it rest, until good is better, and better best.
近期会重新温习一下SSM的相关知识,相应的博客会更新至专栏【SSM框架】中,欢迎大家关注!
SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html
1. 创建user表
2. 创建User实体类
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3. 引入mysql驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
public static List<User> findAll() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
String sql = "select * from user";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
List<User> userList = new ArrayList<User>();
while(rs.next()){
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
userList.add(user);
}
rs.close();
pstmt.close();
conn.close();
return userList;
}
public static void insertUser(User user) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
String sql = "insert into user(id,username,password) values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,user.getId());
pstmt.setString(2,user.getUsername());
pstmt.setString(3,user.getPassword());
int i = pstmt.executeUpdate();
System.out.println(i);
pstmt.close();
conn.close();
}
MyBatis官网地址:http://www.mybatis.org/mybatis-3/
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
同上。
在resources目录下创建文件com/wang/mapper/UserMapper.xml。路径与实体类的包路径对应
<?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="userMapper">
<select id="findAll" resultType="com.wang.pojo.User">
select * from user
</select>
</mapper>
在resources目录下创建文件sqlMapConfig.xml. 并添加相应配置
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 映射文件所在位置-->
<mapper resource="com/wang/mapper/UserMapper.xml"/>
</mappers>
</configuration>
测试
public static void main(String[] args) throws IOException {
//加载核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得sqlSession对象
SqlSession session = sqlSessionFactory.openSession();
//执行sql语句
List<User> userList = session.selectList("userMapper.findAll");
Iterator<User> iterator = userList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
session.close();
}
<insert id="add" parameterType="com.wang.pojo.User">
insert into user(id,username,password) values(#{id},#{username},#{password})
</insert>
@Test
public void add() throws Exception{
//加载核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得sqlSession对象
SqlSession session = sqlSessionFactory.openSession();
//执行sql语句
int insert = session.insert("userMapper.add",new User("Curry","30303030"));
System.out.println(insert);
session.commit();
session.close();
}
3. 注意
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
@Test
public void delete() throws Exception{
//加载核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得sqlSession对象
SqlSession session = sqlSessionFactory.openSession();
//执行sql语句
int delete = session.delete("userMapper.delete",1); //删除id=1的user
System.out.println(delete);
session.commit();
session.close();
}
3. 注意
<update id="update" parameterType="com.wang.pojo.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
@Test
public void update() throws Exception{
//加载核心配置文件
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获得sqlSession对象
SqlSession session = sqlSessionFactory.openSession();
//执行sql语句
int update = session.update("userMapper.update",new User(2,"科比","24-8")); //删除id=2的user
System.out.println(update);
session.commit();
session.close();
}
3. 注意
见上述开发步骤,这里不再重复。
1. environments标签
事务管理类型有两种:
数据源类型有三种:
2. mapper标签
mapper标签用于加载映射文件。
加载方式有如下几种:
<mapper resource="com/wang/mapper/UserMapper.xml"/>
<mapper url="file:///mapper/UserMapper.xml"/>
<mapper class="com.wang.mapper.UserMapper"/>
<package name="com.wang.mapper"/>
3. properties标签
properties标签加载额外配置的properties文件,例如将数据源的配置信息单独抽取成一个properties文件。
4. typeAliases标签
在映射文件中为SQL参数类型或者返回类型的时候,需要配置全包名,这就比较烦人。
这是就可以使用typeAliases标签,为Java 类型设置一个短的名字。
<typeAliases>
<typeAlias type="com.wang.pojo.User" alias="user"/>
</typeAliases>
mybatis框架已经为我们设置好的一些常用的类型的别名
SSM专栏:https://blog.csdn.net/weixin_43598687/category_11652306.html
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43598687/article/details/123822636
内容来源于网络,如有侵权,请联系作者删除!