@Repository
public interface StoryDao extends JpaRepository<Story, Integer> {
@Modifying
@Query("update Story set posId=posId+1 where posId>:posId")
void updateCreatePosID(@Param("posId") int posId);
List<Story> findByTaskIdOrderByPosIdDesc(int taskId);
List<Story> findByTaskId(int taskId);
}
@Service
public class StoryServiceImpl implements StoryService {
@Autowired
private StoryDao storyDao;
}
@Repository
public interface StoryDao extends JpaRepository<Story, Integer> {
@Modifying
@Query("update Story set posId=posId+1 where posId>:posId")
void updateCreatePosID(@Param("posId") int posId);
List<Story> findByTaskIdOrderByPosIdDesc(int taskId);
List<Story> findByTaskId(int taskId);
}
标签名 | 功能 | 备注 |
---|---|---|
@Entity | 用来表示这个类似实体类,(UserEntity) 默认对应user_entity表 | - |
@Column | 定义将成员属性映射到关系表中的对应列,以及对应列的结构信息 | 可以包含name、unique、nullable、length、insertable、length、updatetable、columnDefination、secondaryTable |
@id | 用来标识指定表的逐渐,有多重生成方式 | table(使用低层数据表)、SEQUENCE,使用序列生成唯一、INDENTITY使用数据库IDENTITY列保证唯一、AUTO 由容器挑选一个合适的方式保证唯一、NONE 容器不负责主键的生成,有程序完成 |
@GeneratedValue(strategy = GenerationType.IDENTITY) | 用来指定生成的方式 | - |
TODO
主要步骤如下
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2. 获得数据库连接
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
//3.操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess");
//如果有数据,rs.next()返回true
while(rs.next()){
System.out.println(rs.getString("user_name")+" 年龄:"+rs.getInt("age"));
}
public interface PetDao {
/**
* 查询所有宠物
*/
List<Pet> findAllPets() throws Exception;
}
public class PetDaoImpl extends BaseDao implements PetDao {
/**
* 查询所有宠物
*/
public List<Pet> findAllPets() throws Exception {
Connection conn=BaseDao.getConnection();
String sql="select * from pet";
PreparedStatement stmt= conn.prepareStatement(sql);
ResultSet rs= stmt.executeQuery();
List<Pet> petList=new ArrayList<Pet>();
while(rs.next()) {
Pet pet=new Pet(
rs.getInt("id"),
rs.getInt("owner_id"),
rs.getInt("store_id"),
rs.getString("name"),
rs.getString("type_name"),
rs.getInt("health"),
rs.getInt("love"),
rs.getDate("birthday")
);
petList.add(pet);
}
BaseDao.closeAll(conn, stmt, rs);
return petList;
}
}
public class Pet {
private Integer id;
private Integer ownerId; //主⼈ID
private Integer storeId; //商店ID
private String name; //姓名
private String typeName; //类型
private int health; //健康值
private int love; //爱⼼值
private Date birthday; //⽣⽇
}
public class BaseDao {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://127.0.0.1:3306/database_name";
private static String user="root";
private static String password="root";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void closeAll(Connection conn,Statement stmt,ResultSet rs) throws SQLException {
if(rs!=null) {
rs.close();
}
if(stmt!=null) {
stmt.close();
}
if(conn!=null) {
conn.close();
}
}
public int executeSQL(String preparedSql, Object[] param) throws ClassNotFoundException {
Connection conn = null;
PreparedStatement pstmt = null;
/* 处理SQL,执⾏SQL */
try {
conn = getConnection(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数
}
}
ResultSet num = pstmt.executeQuery(); // 执⾏SQL语句
} catch (SQLException e) {
e.printStackTrace(); // 处理SQLException异常
} finally {
try {
BaseDao.closeAll(conn, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
}
spring:
datasource:
url: jdbc:mysql://localhost:3306/Hotel?
serverTimezone=CTT&characterEncoding=UTF-8
username: root
password: ******
driver-class-name: com.mysql.cj.jdbc.Driver
max-active: 200
max-idle: 20
min-idle: 10
thymeleaf:
cache: false
jackson:
time-zone: GMT+8
mybatis:
mapper-locations: classpath:dataImpl/*/*Mapper.xml
@Mapper
@Repository
public interface AdminMapper {
int addManager(User user);
List<User> getAllManagers();
}
public class User { //省略getter、setter
private Integer id;
private String email;
private String password;
private String userName;
private String phoneNumber;
private double credit;
private UserType userType;
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.hotel.data.admin.AdminMapper">
<insert id="addManager" parameterType="com.example.hotel.po.User" useGeneratedKeys="true" keyProperty="id">
insert into User(email,password,usertype)values(#{email},#{password},#{userType})
insert>
<select id="getAllManagers" resultMap="User">
select * from User where usertype='HotelManager'
select>
<resultMap id="User" type="com.example.hotel.po.User">
<id column="id" property="id">id>
<result column="email" property="email">result>
<result column="password" property="password">result>
<result column="username" property="userName">result>
<result column="phonenumber" property="phoneNumber">result>
<result column="credit" property="credit">result>
<result column="usertype" property="userType">result>
resultMap>
mapper>
关系型数据库 | ⾯向对象 |
---|---|
数据库的表(table) | 类(class) |
记录(record,⾏数据) | 对象(object) |
字段(field) | 对象的属性(attribute) |
SELECT id, first_name, last_name, phone, birth_date, sex
FROM persons
WHERE id = 10
res = db.execSql(sql);
name = res[0]["FIRST_NAME"];
p = Person.get(10);
name = p.first_name;