从persistent.xml 获取数据库用户名和密码值

时间:2020-12-22 11:39:36

标签: java spring-mvc entitymanager persistence.xml

我正在尝试获取要在我的应用程序中使用的数据库用户名和密码值。我正在使用 EntityManager ,但是当打印这些值时,我用星号隐藏了它们。如何在没有星号的情况下隐藏这些值?

控制器类

@Controller
public class AttendanceReportController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ShiftMasterService shiftMasterService;

    @Autowired
    private EmployeeAttendanceService employeeAttendanceService;

    @PersistenceContext
    EntityManager entityManager;

    @GetMapping("/AttendanceReport")
    public String AttendanceReportPage() {
        return "attendanceReport";
    }

    @ModelAttribute("depList")
    public List<DepartmentMaster> getAllDeps() throws SQLException {

        EntityManagerFactory emf = entityManager.getEntityManagerFactory();
        Map<String, Object> emfProperties = emf.getProperties();

        String url = (String) emfProperties.get("javax.persistence.jdbc.url");
        String user = (String) emfProperties.get("javax.persistence.jdbc.user");
        String password = (String) emfProperties.get("javax.persistence.jdbc.password");
        System.out.println("URL : " + url);
        System.out.println("Username : " + user);
        System.out.println("Password : " + password);

        return departmentService.getAllDep();
    }
}

persistent.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="hrm">
<properties>
   <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hrm"/>
   <property name="javax.persistence.jdbc.user" value="root"/>
   <property name="javax.persistence.jdbc.password" value="1234"/>
   <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
   
   <property name="hibernate.show_sql" value="true" ></property>
   <property name="hibernate.format_sql" value="true" />
   
   <property name="hibernate.jdbc.batch_size" value="30" />
   <property name="hibernate.order_inserts" value="true" />
   <property name="hibernate.generate_statistics" value="false" />
   
</properties>


</persistence-unit>

</persistence>

输出

URL : jdbc:mysql://localhost:3306/hrm
Username : ****
Password : ****

Result Image

0 个答案:

没有答案